HttpClient的不存储饼干的CookieContainer [英] HttpClient not storing cookies in CookieContainer

查看:360
本文介绍了HttpClient的不存储饼干的CookieContainer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 VS2010 + .NET 4.0 + System.Net.Http (从的NuGet )。



有关,我不管理的理由明白了,这是我收到的会话cookie我的 HttpResponseMessage 不会自动保存在的HttpClient的CookieContainer
这里是我的代码如下:

 的CookieContainer饼干=新的CookieContainer(); 
HttpClientHandler处理程序=新HttpClientHandler();
handler.CookieContainer =饼干;
HttpClient的客户端=新的HttpClient(处理);

乌里网站=新的URI(https://www.mywebsite.com);
VAR response1 = client.SendAsync(新HttpRequestMessage(HttpMethod.Get,网站))结果。





我可以在我有以下的响应头看
<预类=郎无prettyprint-覆盖> 设置Cookie:JSESSIONID = FC8110E434C2C6DAB78B4E335024A639;路径= /成员;安全



不过我的cookie容器是空的......为什么?


< DIV CLASS =h2_lin>解决方案

我想这个问题是,你的cookies的安全的。问题在于,的CookieContainer不会发送安全cookie返回到在随后的HTTP请求的服务器。这可能是一个错误,或者它有它背后的一些原因。



一个解决方法是重新添加cookie来的CookieContainer手动。这样一来,饼干将在HTTP请求头被送回,因为没有任何的安全的,当你发送cookie回服务器将被定义。



请参见更多信息,这篇文章


I'm using VS2010 +.NET 4.0 + System.Net.Http (from Nuget).

For a reason which I don't manage to understand, the session cookie which I receive in my HttpResponseMessage is not automatically saved in the HttpClient CookieContainer. Here is what my code looks like:

CookieContainer cookies = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookies;
HttpClient client = new HttpClient(handler);

Uri site = new Uri("https://www.mywebsite.com");
var response1 = client.SendAsync(new HttpRequestMessage(HttpMethod.Get,site)).Result;

I can see in the response headers that I have the following:

Set-Cookie: JSESSIONID=FC8110E434C2C6DAB78B4E335024A639; Path=/member; Secure

However my cookie container remains empty ...why ?

解决方案

I guess the problem is that your cookies are secure. The problem is that, CookieContainer won't send secure cookies back to the server in subsequent HTTP requests. It might be a bug, or maybe it has some reasons behind it.

A workaround is to re-add the cookie to CookieContainer manually. This way, cookie would be sent back in HTTP request header, as no secure would be defined when you send cookies back to the server.

See this article for more information.

这篇关于HttpClient的不存储饼干的CookieContainer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆