HttpClient is adding it's own cookie header

China☆狼群 提交于 2019-12-06 06:01:39

问题


I have rather strange problem. With all that questions over internet how to add and get cookies, I want the opposite ;)

When I try to send request via HttpHandler it is adding it's own Cookie header. I have to get rid of it. Without digging into details - when it is added, server I am trying to request is giving me wrong answer. It works without this cookie (tried in fiddler).

But back to the problem, code:

    string domain = "someMysteriousDomain";
    var handler = new HttpClientHandler();
    handler.UseDefaultCredentials = false;
    handler.AllowAutoRedirect = true;
    handler.ClientCertificateOptions = ClientCertificateOption.Manual;
    handler.UseCookies = false;

    var httpClient = new HttpClient(handler);

    var request = new HttpRequestMessage(HttpMethod.Get, domain);
    request.Headers.UserAgent.Add(new ProductInfoHeaderValue("Mozilla", "5.0"));
    request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));

    var response = await httpClient.SendAsync(request);

Raw request seen in fiddler:

GET https://domain HTTP/1.1
Accept-Encoding: gzip
User-Agent: Mozilla/5.0
Host: domain
Connection: Keep-Alive
Cookie: cadata477E7C1824F44800AF0077724F65345="51595d316-0286-44bb-bc6f-ffb1fd311a92SqJA36rA69YW7aBg+iHXYi9LAcBLN6DBWE8a3MLejd2VCluO/UQ5eF6F6T4NWh4NhdRcv4rea15Hs0e2q6GatMac59UVbljhREYdH6PRbzZC/2qn8QHtpc6go5B56R"; mobile=0

I don't want to add that cookie! How to delete/clear/whatever it? I am using Visual Studio Community 2015, with Windows Universal Project. What is interesting, after rebooting my pc after few hours, I was able to make 2 or 3 requests without this cookie (using THE SAME code) and then mysterios cookie returned. What it is about? How to get rid of it?


回答1:


Thank you for reporting this issue - this is a known issue with the System.Net.Http.HttpClientHandler API implementation on Windows 10 and we are working on fixing it in an upcoming release.

In the meanwhile, a possible workaround is to use the Windows.Web.Http.HttpClient API with the underlying HttpBaseProtocolFilter class. This class has a property called CookieManager that stores all the cookies for each URI. You can write a method to delete the cookies from the CookieManager for the destination URI before sending a request. This will ensure that no cookies get sent. You can see this sample for how to delete cookies from the CookieManager: https://github.com/Microsoft/Windows-universal-samples/tree/master/httpclient

Thanks Sidharth [MSFT]



来源:https://stackoverflow.com/questions/31549501/httpclient-is-adding-its-own-cookie-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!