【发布时间】:2013-01-28 15:44:08
【问题描述】:
我正在尝试使用 C# HttpClient 类发送带有 cookie 的 Http Get 消息。
HttpClient.GetAsync 只接受 URL。所以,我不能使用那种方法。
我发现HttpClient.SendAsync 方法让我可以通过以下方式发送cookie。
HttpRequestMessage GETRequest = new HttpRequestMessage(HttpMethod.Get, completeUrl);
GETRequest.Content = new FormUrlEncodedContent(formVals);
HttpResponseMessage GETResponse = client.SendAsync(GETRequest).Result;
但是当执行时,它会说 -
“无法发送具有此动词类型的内容主体”
我知道 Get 消息最适合用于获取而不是用于发布数据。但在我的情况下,cookie 存储在客户端以保存客户端首选项,我必须通过 Http Get 消息(通过 HttpClient 类)将它们发送给客户端。
我不想重新初始化 HttpClient 对象;这将改变它的 SessionID。 我不想将内容添加到获取消息。我只是问是否有任何其他方法可以使用附加值更新获取消息 cookie。
请告诉我如何处理这个问题。提前感谢您的帮助。
【问题讨论】:
-
您认为可以将 cookie 作为消息内容发送的假设是错误的。 Cookie 以 HTTP 标头字段的形式发送。
-
好吧,我说它是 cookie,因为我在 Fiddler 的“Cookie”属性中看到它。我将在下一篇文章中发布实际的 Get 请求响应。
-
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=查询字符串=http%3a%2f%2fdc1lohgaonkarlt%2fLPN%2fTeacher%2fBrowseMaterials.aspx;ASP.NET_SessionId=t1xadfmys5tmoz3bwnwginnw;搜索=“;第二次获取味精 - “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=" - 在第二次请求内容中添加了第一次获取请求。
-
kmatyaszek - 你明白我的问题吗?请参阅 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;时区偏移=5;客户=MN; AirUser=SessionId=0ce9b203…………
-
这篇文章能回答你的问题吗? msdn.microsoft.com/en-us/library/dd920298(v=VS.95).aspx
标签: c# http-headers asp.net-web-api dotnet-httpclient sendasync