问题描述
运行UWP应用*
所以我有一个HttpClient及其关联的处理程序。我正在向网站发出请求,传入指定的标头,并使用指定的CookieContainer,该Cookie在请求开始时为空。
So I have an HttpClient and it's associated handler. I am making a request to a website, passing in specified headers, and using a specified CookieContainer, which is empty at the beginning of the request.
当我发送请求时,Fiddler显示正在发送的其他我尚未添加的cookie。他们来自哪里?
When I send the request, Fiddler shows extra cookies being sent that I have not added. Where are they coming from?
CookieContainer cookieJar = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler( );
handler.UseDefaultCredentials = false;
handler.CookieContainer = cookieJar;
handler.Proxy = null;
using (var client = new HttpClient(handler as HttpClientHandler))
{
HttpResponseMessage response = new HttpResponseMessage();
String loginUrl = Const.BUNGIE_LOGIN_URI + (provider == Const.XBOX_PLATFORM ? Const.XBOX_LOGIN : Const.PS_LOGIN);
client.BaseAddress = new Uri(loginUrl);
//client.Timeout = new TimeSpan(0, 0, 0, 0, 450);
client.DefaultRequestHeaders.Add("Referer", "http://www.bungie.net");
client.DefaultRequestHeaders.Add("User-Agent", Const.USERAGENT);
client.DefaultRequestHeaders.Add("X-API-Key", Const.X_API_KEY);
client.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("Accept-Language", "en-GB,en-US;q=0.8,en;q=0.6");
handler.AutomaticDecompression = DecompressionMethods.GZip;
response = client.GetAsync("").Result;
response.ReadCookies(); //adds cookies to cookieJar
}
现在,作为关联的 CookieContainer 为空,这些cookie来自何处?他们可以到达吗?如果我想从中获得价值,我将如何获得它们?
Now, as the associated CookieContainer
is empty before the request is made, where are these cookies coming from? Are they accessible? If I wanted the values from them, how would I obtain them?
编辑:它们是从哪里添加到我的HttpClient请求中的? HttpClient是否具有通用的CookieContainer /缓存?我有两个单独的HttpClient实例,当客户端(A)发出请求时,它收到一个 set-cookie标头,并设置了一个乱七八糟的cookie。
Where are they being added to my HttpClient request from? Does HttpClient have a common CookieContainer / cache? I have two separate HttpClient instances, and when Client (A) makes a request, it received a "set-cookie" header back, setting a "bungled" cookie.
稍后,作为HttpClient的一个单独实例,客户端(B)向同一网站发出请求,并发送客户端(A)中设置的cookie。
Later on, a separate instance of HttpClient, Client (B), makes a request to the same website, sending the cookie set within Client (A).
我没有将此Cookie明确附加到客户(B)的请求中,如何添加它?
I did not explicitly append this cookie to Client (B)'s request, so how is it being added?
推荐答案
在Windows 10内部版本10240上, System.Net.Http.HttpClient
是 Windows.Web.Http.HttpClient
(),因此,cookies现在的工作方式略有不同。
On Windows 10 build 10240, System.Net.Http.HttpClient
is a wrapper on top of Windows.Web.Http.HttpClient
(more info here), and so, cookies now work little bit different.
要删除这些多余的cookie,您将需要使用 HttpCookieManager
删除cookie:
To delete those extra cookies, you will need to delete the cookies using a HttpCookieManager
:
HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
HttpCookieManager cookieManager = filter.CookieManager;
foreach (HttpCookie cookie in cookieManager.GetCookies(uri))
{
cookieManager.DeleteCookie();
}
这篇关于HttpClient发送额外的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!