HttpClient正在发送额外的cookie

时间:2015-11-21 22:54:04

标签: c# cookies httpclient fiddler uwp

运行UWP app *

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

当我发送请求时,Fiddler会显示我尚未添加的额外Cookie。他们来自哪里?

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
 }

What fiddler shows

现在,由于关联的CookieContainer在请求发出之前是空的,这些cookie来自哪里?他们可以访问吗?如果我想要它们的价值,我将如何获得它们?

编辑:他们从哪里添加到我的HttpClient请求中? HttpClient有一个共同的CookieContainer /缓存吗?我有两个单独的HttpClient实例,当客户端(A)发出请求时,它收到一个“set-cookie”标头,设置一个“拙劣的”cookie。

稍后,HttpClient的另一个实例Client(B)向同一网站发出请求,在Client(A)中发送cookie集。

我没有明确地将此cookie附加到客户端(B)的请求中,那么它是如何添加的呢?

1 个答案:

答案 0 :(得分:5)

在Windows 10内部版本10240上,Windows.Web.Http.HttpClientHttpCookieManagermore info here)之上的包装器,因此,Cookie现在的工作方式略有不同。

要删除这些额外的Cookie,您需要使用HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter(); HttpCookieManager cookieManager = filter.CookieManager; foreach (HttpCookie cookie in cookieManager.GetCookies(uri)) { cookieManager.DeleteCookie(); }

删除Cookie
<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Memberlogin extends CI_Controller {

//protected $CI;

function __construct()
{
    parent::__construct();

    $this->load->library('session');
    //$this->load->library('form_validation');
    //$this->load->helper(array('form', 'url'));
    $this->load->helper('date');
    $this->load->helper('ip_address');
    $this->load->model('Sql_model');
}