1

我想用 Rest Webservice 做一个 POST。该服务是使用 NetweaverGateway 构建的,用于 SAP 后端。为了做一个帖子,首先需要做一个带有标题'X-CSRF'值的GET作为Fetch。我将获得这个标题的一个值并且需要在POST期间传递。我还将获得一个Set-Cookie的值header. 并且需要在 POST 期间作为 Cookie 标头应用。

在 Windows Phone 的情况下,Set-Cookie Header 值如下所示。

MYSAPSSO2=AjQxMDMBABhBAFAAUABEAEUAVgBFAEwATwBQACAAIAACAAY4ADAAMAADABBJAEQANQAgACAAIAAgACAABAAYMgAwADEANAAwADEAMQA0ADEAMgAzADcABQAEAAAACAYAAlgACQACRQD%2fAVYwggFSBgkqhkiG9w0BBwKgggFDMIIBPwIBATELMAkGBSsOAwIaBQAwCwYJKoZIhvcNAQcBMYIBHjCCARoCAQEwbzBkMQswCQYDVQQGEwJERTEcMBoGA1UEChMTU0FQIFRydXN0IENvbW11bml0eTETMBEGA1UECxMKU0FQIFdlYiBBUzEUMBIGA1UECxMLSTAwMjA1NzgyMDkxDDAKBgNVBAMTA0lENQIHIBIRIAZSCDAJBgUrDgMCGgUAoF0wGAYJKoZIhvcNAQkDMQsGCSqGSIb3DQEHATAcBgkqhkiG9w0BCQUxDxcNMTQwMTE0MTIzNzI2WjAjBgkqhkiG9w0BCQQxFgQUl9pQNO3%21xAL961k5uldlrK9v4F4wCQYHKoZIzjgEAwQvMC0CFQC1Y2P0KidZPdz9cEDXfcJQXfaZHQIUSrgfviQp8crDmko2WfC5fRDIgLc%3d; 路径=/; 域=.168.3.10,SAP_SESSIONID_ID5_800=2kvgSUGIpZ2YFXaubeuff5zTKwp9GBHjn-AAJlUa2wA%3d;路径=/

在 Windows 控制台应用程序的情况下,Set-Cookie Header 值如下所示。

MYSAPSSO2=AjQxMDMBABhBAFAAUABEAEUAVgBFAEwATwBQACAAIAACAAY4ADAAMAADABBJAEQANQAgACAAIAAgACAABAAYMgAwADEANAAwADEAMQA0ADEAMgAzADkABQAEAAAACAYAAlgACQACRQD%2fAVYwggFSBgkqhkiG9w0BBwKgggFDMIIBPwIBATELMAkGBSsOAwIaBQAwCwYJKoZIhvcNAQcBMYIBHjCCARoCAQEwbzBkMQswCQYDVQQGEwJERTEcMBoGA1UEChMTU0FQIFRydXN0IENvbW11bml0eTETMBEGA1UECxMKU0FQIFdlYiBBUzEUMBIGA1UECxMLSTAwMjA1NzgyMDkxDDAKBgNVBAMTA0lENQIHIBIRIAZSCDAJBgUrDgMCGgUAoF0wGAYJKoZIhvcNAQkDMQsGCSqGSIb3DQEHATAcBgkqhkiG9w0BCQUxDxcNMTQwMTE0MTIzOTIyWjAjBgkqhkiG9w0BCQQxFgQU7c96Mnc03Z93lKsX929FWOrxYdswCQYHKoZIzjgEAwQvMC0CFQDXaD%2frrpLCCvzU85bLT1Km4NBUgAIUUwVjlcngGRjw5O3iPAr8GST2Oww%3d; 路径=/; 域=.168.3.10,SAP_SESSIONID_ID5_800=dbbs5TYL4SVjK4oZuPu5W-RZtwh9GBHjn-AAJlUa2wA%3d; 路径=/,sap-XSRF_ID5_800=3_UuV7y6z8bidVbm0rszQQ%3d%3d20140114123922ryOO43OgtywearpQUbML8YgnytbuW3-Qs0Jt4tuOgcc%3d; 路径=/; HttpOnly

我需要来自 Set-Cookie 的 sap-XSRF_ID5_800 标头值才能发布。不幸的是,在 Windows Phone 的情况下没有这样的值。而且我在发布时遇到身份验证错误。

在控制台应用程序的情况下也是如此。

在这方面需要帮助。我尝试使用此处指定的解决方案 Preserving HTTPOnly cookies on Windows Phone

但我无法访问 cookie。

更新:

下面是我的代码

    string xToken = "";
                byte[] auth = Encoding.UTF8.GetBytes(UserName + ":" + Pwd);
                string postXml = "xml to post";


                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.example.com/Headers");

                byte[] byteArray = Encoding.UTF8.GetBytes(postXml);
                request.ContentType = "application/atom+xml";
                request.Method = "GET";
                request.Headers["Authorization"] = "Basic " + System.Convert.ToBase64String(auth);
                request.Headers["X-CSRF-Token"] = "Fetch";



                HttpWebResponse responseH = (HttpWebResponse)request.GetResponse();
                xToken = responseH.Headers["X-CSRF-Token"];
                responseH.Close();
                request.Method = "POST";
                request.Headers["Authorization"] = "Basic " +    System.Convert.ToBase64String(auth);
                request.Headers["X-CSRF-Token"] = xToken;

 Stream dataStream1 = request.GetRequestStream();
                dataStream1.Write(byteArray, 0, byteArray.Length);
                dataStream1.Close();
                HttpWebResponse response1 = (HttpWebResponse)request.GetResponse();
                Console.WriteLine(((HttpWebResponse)response1).StatusDescription);
                dataStream1 = response1.GetResponseStream();
                StreamReader reader1 = new StreamReader(dataStream1);
                string responseFromServer1 = reader1.ReadToEnd();
                Console.WriteLine(responseFromServer1);
                Console.Read();
                reader1.Close();
                dataStream1.Close();
                response1.Close();
4

1 回答 1

0

您不应该访问 cookie,也不需要访问。只要您使用与 GET 相同的 HttpClient 实例来进行 POST,那么它们将共享具有 CookieContainer 的相同 HttpClientHandler。

对 GET 的响应中的 Set-Cookie 标头会将 cookie 添加到 cookie 容器中,并且您的下一个 POST(使用相同的 HttpClient 实例)会将 cookie 添加到标头中。

于 2014-01-14T13:53:25.507 回答