1

I am trying to use C# HttpClient class to send Http Get message with cookies. HttpClient.GetAsync just accepts URL. So, I cannot use that method. I found that HttpClient.SendAsync method let me send cookies in following way.

HttpRequestMessage GETRequest = new HttpRequestMessage(HttpMethod.Get, completeUrl);
GETRequest.Content = new FormUrlEncodedContent(formVals);
HttpResponseMessage GETResponse = client.SendAsync(GETRequest).Result;

But when executed, it fails saying -

"Cannot send a content-body with this verb-type"

I know that Get messages are ideally for getting and not for Posting data. But in my situation, the cookie is stored on client to save client preferences and I have to send them to client through Http Get message (through HttpClient class).

I don't want to reinitialize HttpClient object; that will change its SessionID. I don't want to add content to Get message. I am just asking is there is any other way to update Get message cookie with additional values.

Please advise me how do I deal with this problem. Thanks for your help in advance.

Srk
  • 21
  • 1
  • 6
  • Your assumption, that you can send cookies as message Content, is wrong. Cookies are sent as HTTP header fields. – dtb Jan 28 '13 at 15:46
  • Well, I am saying it cookie because I am seeing it in "Cookie" attribute in Fiddler. I will post actual Get request response in next post. – Srk Jan 28 '13 at 15:50
  • 1st GET msg- "Cookie: Mode=Teacher; TimeZoneOffset=5; Client=MN; AirUser=SessionId=0ce9b203-66aa-43a9-8476-521d6b33b392&TeacherId=6584&StudentId=0&Name=DAC DemoTwo; querystring=querystring=http%3a%2f%2fdc1lohgaonkarlt%2fLPN%2fTeacher%2fBrowseMaterials.aspx; ASP.NET_SessionId=t1xadfmys5tmoz3bwnwginnw; search=" ; 2nd Get msg - "Cookie: ....search=viewall=1&grades=&materialType=&itemSearchControl0=itm_att_Item FORMAT,SIM&itemSearchControl1=itm_att_Learning Modalities&itemSearchControl2=itm_att_Flesch-Kincaid Grade Level Readability&text=" - In second req content is in addtion to 1st Get request. – Srk Jan 28 '13 at 15:54
  • kmatyaszek - did you understand my problem? see the sample Get message in Fiddler -->> GET xxxxx/Callback.aspx?method=GetSearchResults&page=1&rnd=0.1948752427207086 HTTP/1.1 Accept: */* Accept-Language: en-us Referer: xxxxx/BrowseMaterials.aspx Accept-Encoding: gzip, deflate User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0) Host: xxx Connection: Keep-Alive Cookie: Mode=Teacher; TimeZoneOffset=5; Client=MN; AirUser=SessionId=0ce9b203......... – Srk Jan 28 '13 at 16:23
  • Does this article answer your question? http://msdn.microsoft.com/en-us/library/dd920298(v=VS.95).aspx – MisterIsaak Jan 28 '13 at 19:47
  • Unfortunately above link doesn't answer my question. I have to use WebAPI HttpClient class to send these request. I am tracking multiple requests through same instance of the class (i.e. multiple requests for a particular sessionID). First Get request creates SessionID; its HttpResponseMessage has sessionID, cookie header, etc. elements. For second request, I have to add additional data to returned cookie in the header of first request message. HttpClient class provides only two methods for Get - GetAsync(URL) and SendAsync(HttpRequestMessage). Now, I have to somehow update the cookie header. – Srk Jan 28 '13 at 21:02

1 Answers1

3

You should use the CookieContainer class, just as shown in this answer. This will set the cookies in the HTTP header (where they should be placed).

Community
  • 1
  • 1
csima
  • 315
  • 2
  • 14
  • Thanks for your reply. I tried that CookieContainer class but that will a POST message. I need GET (as in GetAsync or SendAsync with HttpMethod.Get). I tried adding "Cookie" in the header but it is silently ignored. It may be because "Cookie" already exists in the message. If that is the way to go, then I am not how to edit/update "Cookie" attribute in the message header. – Srk Jan 28 '13 at 15:59
  • I am so exhausted with this problem. It is sounds small but I am not able to find a way of this. Please comment how to get around this situation. – Srk Jan 28 '13 at 18:56
  • 2
    @srk Did you actually try it? From my tests the CookieContainer works just the same with GET as with POST. And if you really don't want to use the cookie container the next answer to the referenced question addresses that. – Darrel Miller Jan 28 '13 at 19:39
  • Yes, I did. I don't understand how can they mark as a duplicate question. My HttpClient class object doesn't take any argument and I cannot reinitialize it class object with new cookies. That will reset its SessionID(I don't want it to change). – Srk Jan 28 '13 at 20:44
  • Darrel, just curious. How do you pass CookieContainer to Get message using HttpClient class and its methods? There are only two methods - GetAsync which takes only URI and hence not useful. SendAsync takes HttpRequestMessage and here you cannot use HttpClientHandler without reintializing HttpClient class object. That is the trap I am into. Please reply. Thanks. – Srk Jan 28 '13 at 21:28
  • 1
    @srk Using either GetAsync or SendAsync will send cookies if the cookiecontainer is setup. I don't understand your issue with HttpClientHandler. Here's a gist https://gist.github.com/4659969 we can continue the comment thread there... – Darrel Miller Jan 28 '13 at 22:39