HttpClientHandler 和淘宝 cookie的坑

HttpClientHandler 和淘宝 cookie的坑

handler = new HttpClientHandler()
{
UseCookies =true,
AllowAutoRedirect = true,
Proxy = null,
UseProxy = false,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
UseCookies、AllowAutoRedirect两个属性设置为真值时,HttpClient进行HTTP通信, handler的CookieContainer将自动更新服务器响应的Set-Cookie,包括跨域跳转,看起来很方便。但是淘宝一个请求的响应头有多个Set-Cookie值,而且不归范,不是名=值的形式;而是有这样的:
uc1=cookie16=URm48syIJ1yk0MX2J7mAAEhTuw%3D%3D&cookie21=V32FPkk%2FgihF%2FS5nr3O5&cookie15=U%2BGCWk%2F75gdr5Q%3D%3D&existShop=false&pas=0&cookie14=UoTYMbyX6zcquQ%3D%3D&tag=8&lng=zh_CN; Domain=.taobao.com; Path=/,一个cookie有多个等号。这种cookie,CookieContainer是不会识别和更新的。所以还得用httpClient.DefaultRequestHeaders.Add(“Cookie”, cookstr)这样的代码来管理cookie.

你可能感兴趣的:(代码)