1

I want do a POST with Rest Webservice. The service is build using NetweaverGateway and is used for SAP backend. Inorder to do a post, firstly need to do a GET with header 'X-CSRF' value as Fetch.I will be getting a value for this header and need to pass during POST.Also i will be getting a value for Set-Cookie header.And need to apply as Cookie header during POST.

Set-Cookie Header value is as below in case of Windows Phone.

MYSAPSSO2=AjQxMDMBABhBAFAAUABEAEUAVgBFAEwATwBQACAAIAACAAY4ADAAMAADABBJAEQANQAgACAAIAAgACAABAAYMgAwADEANAAwADEAMQA0ADEAMgAzADcABQAEAAAACAYAAlgACQACRQD%2fAVYwggFSBgkqhkiG9w0BBwKgggFDMIIBPwIBATELMAkGBSsOAwIaBQAwCwYJKoZIhvcNAQcBMYIBHjCCARoCAQEwbzBkMQswCQYDVQQGEwJERTEcMBoGA1UEChMTU0FQIFRydXN0IENvbW11bml0eTETMBEGA1UECxMKU0FQIFdlYiBBUzEUMBIGA1UECxMLSTAwMjA1NzgyMDkxDDAKBgNVBAMTA0lENQIHIBIRIAZSCDAJBgUrDgMCGgUAoF0wGAYJKoZIhvcNAQkDMQsGCSqGSIb3DQEHATAcBgkqhkiG9w0BCQUxDxcNMTQwMTE0MTIzNzI2WjAjBgkqhkiG9w0BCQQxFgQUl9pQNO3%21xAL961k5uldlrK9v4F4wCQYHKoZIzjgEAwQvMC0CFQC1Y2P0KidZPdz9cEDXfcJQXfaZHQIUSrgfviQp8crDmko2WfC5fRDIgLc%3d; path=/; domain=.168.3.10, SAP_SESSIONID_ID5_800=2kvgSUGIpZ2YFXaubeuff5zTKwp9GBHjn-AAJlUa2wA%3d; path=/

Set-Cookie Header value is as below in case of Windows Console Application.

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

I need sap-XSRF_ID5_800 header value from Set-Cookie in order to POST.Unfortunately there is no such value in case of Windows Phone.And Iam getting a authentication error while posting.

The same thing works in case of console application.

Need help regarding this. I tried with solution specified here Preserving HTTPOnly cookies on Windows Phone

But i cant access the cookies.

Update:

Below is my code

    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();
Community
  • 1
  • 1
Praveen
  • 267
  • 1
  • 9

1 Answers1

0

You aren't supposed to access the cookies and you shouldn't need to. As long as you use the same instance of HttpClient to make the POST as you made the GET with then they will share the same HttpClientHandler which has a CookieContainer.

The Set-Cookie header on the response to the GET will add a cookie to the cookie container and your next POST (with the same HttpClient instance) will add the cookie into the header.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • If i use the same instance of HttpClient ,at the time of POST when im setting the ContentLength im getting a invalid operation exception "This property cannot be set after writing has started". – Praveen Jan 15 '14 at 06:07
  • @Praveen You never need to set the content-length header. It will be set for you by the framework. – Darrel Miller Jan 15 '14 at 12:25
  • if i do so im getting an exception "Cannot send a content-body with this verb-type." while writing data to the stream – Praveen Jan 15 '14 at 13:09
  • I have updated my question with my code.I there is any mistake please let me know. – Praveen Jan 15 '14 at 13:15