Web请求和c#请求之间的Cookie计数差异

时间:2017-04-29 12:31:06

标签: c# asp.net

我有一个https网站,当我通过浏览器访问它并检查cookie(通过firebug)时,它给了我3个cookie。但是当我通过c#代码发出请求时,cookie计数为2.其中一个缺失。有人可以帮助发生什么事吗?

public void Login(string url)
    {


        //Create request to URL.
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create( url);

        //Set request headers.
        request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0";
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.5");
        request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
        request.ContentType = "application/x-www-form-urlencoded";
        request.KeepAlive = true;
        request.Headers.Add("Upgrade-Insecure-Requests", @"1");

        //Set request method
        request.Method = "POST";

        request.ServicePoint.Expect100Continue = false;

        // Set request body.
        string body = @"";
        byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
        request.ContentLength = postBytes.Length;
        Stream stream = request.GetRequestStream();
        stream.Write(postBytes, 0, postBytes.Length);
        stream.Close();


        CookieContainer cookieJar = new CookieContainer();
        request.CookieContainer = cookieJar;

        //Get response to request.
        var response = (HttpWebResponse)request.GetResponse();

        var xx = ReadResponse(response);
        int cookieCount = cookieJar.Count;

        var items = cookieJar.List();
        fixCookies(response);
        items = cookieJar.List();


    }



    private static void fixCookies(HttpWebResponse response)
    {
        for (int i = 0; i < response.Headers.Count; i++)
        {
            string name = response.Headers.GetKey(i);
            if (name != "Set-Cookie")
                continue;
            string value = response.Headers.Get(i);
            foreach (var singleCookie in value.Split(','))
            {
                //I get only two values here. 
            }
        }
    }

0 个答案:

没有答案