Answered by:
Windows.Web.Http.HttpClient doesn't store cookies from Set-Cookie response

Question
-
I'm using Windows.Web.Http.HttpClient in a universal app to access some web API, and I noticed that on some of the responses' headers, there are Set-Cookie items which are values that needs to be sent on subsequent API calls. However, I'm not seeing those values set on subsequent API calls. I'm sure I missed something but I can't figure out what I missed. Here's the code I used:
using Windows.Web.Http.HttpClient; HttpBaseProtocolFilter _httpFilter = new HttpBaseProtocolFilter(); HttpClient _httpClient = new HttpClient(_httpFilter); //Is this necessary to include filter? string _urlBase = "https://my.hostname.com/api/" Uri uri = new Uri(_urlBase+"login"); HttpRequestMessage req_msg1 = new HttpRequestMessage(HttpMethod.Post, uri); req_msg1.Content = new HttpStringContent(somestring); req_msg1.Content.Headers.ContentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("text/xml; charset=UTF-8"); req_msg1.Headers.Host = new Windows.Networking.HostName("my.hostname.com"); HttpResponseMessage rsp_msg1 = await _httpClient.SendRequestAsync(req_msg1); //rsp_msg1 will contain Set-Cookie header like this: //Set-Cookie: JSESSIONID=7EF8967C290413A4354AFD1A6DFF9FC8; Path=/api; HttpOnly //Set-Cookie: KEY1=VALUE1; path=/ Uri uri2 = new Uri(_urlBase+"check"); HttpRequestMessage req_msg2 = new HttpRequestMessage(HttpMethod.Post, uri2); req_msg2.Content = new HttpStringContent(someotherstring); req_msg2.Content.Headers.ContentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("text/xml; charset=UTF-8"); req_msg2.Headers.Host = new Windows.Networking.HostName("my.hostname.com"); HttpResponseMessage rsp_msg2 = await _httpClient.SendRequestAsync(req_msg2);
What I'd like to see is that req_msg2 should contain the cookies with values from rsp_msg1. What should I do/should I not do?
Tuesday, April 7, 2015 3:27 AM
Answers
-
Somehow the code springs back to life and worked as intended with no change at all...Things are working fine now. Thanks for the help.
- Marked as answer by Matt SmallMicrosoft employee, Moderator Wednesday, April 8, 2015 3:53 PM
Wednesday, April 8, 2015 1:38 PM
All replies
-
You haven't missed anything except resending the cookies. The cookies are not persisted in an HTTPClient call - you must manually resend them.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Tuesday, April 7, 2015 1:25 PMModerator -
You haven't missed anything except resending the cookies. The cookies are not persisted in an HTTPClient call - you must manually resend them.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.
So in my case, do I have to manually parse the response header from rsp_msg1, and add the cookies to HttpFilter? Also, if I use HttpFilter's CookieManager, can I set the cookies once and they will be applied to all further HttpClient calls?
Tuesday, April 7, 2015 4:09 PM -
I completely overlooked the Filter aspect of this issue. Let me get a person who is better at HttpClient code to check out this post.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Wednesday, April 8, 2015 12:59 PMModerator -
Somehow the code springs back to life and worked as intended with no change at all...Things are working fine now. Thanks for the help.
- Marked as answer by Matt SmallMicrosoft employee, Moderator Wednesday, April 8, 2015 3:53 PM
Wednesday, April 8, 2015 1:38 PM