HttpClient 正在发送额外的 cookie [英] HttpClient is sending extra cookies

查看:38
本文介绍了HttpClient 正在发送额外的 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行 UWP 应用*

所以我有一个 HttpClient 和它的关联处理程序.我正在向网站发出请求,传入指定的标头,并使用指定的 CookieContainer,该 CookieContainer 在请求开始时为空.

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 build 10240 上,System.Net.Http.HttpClientWindows.Web 之上的包装器.Http.HttpClient (此处有更多信息),因此,cookie 现在的工作方式略有不同.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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