【问题标题】:HttpWebRequest sends 1 cookie instead of 2HttpWebRequest 发送 1 个 cookie 而不是 2 个
【发布时间】:2011-05-06 18:45:25
【问题描述】:

我的程序应该登录到一个站点。要在那里登录,您应该向主页发出请求并获取 cookie,然后您应该登录 (http://site.com/auth/login)。当我向主页发出请求并获取 cookie 时,一切正常,cookie 容器中有两个 cookie,但是当我登录时,只有一个 cookie。代码如下:

    public CookieContainer GetCookies()
    {
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://site.com/");
        httpWebRequest.CookieContainer = new CookieContainer();
        httpWebRequest.Accept = "*/*";
        httpWebRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, "ru");
        httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; WebMoney Advisor; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Tablet PC 2.0; .NET4.0C; .NET CLR 1.1.4322; .NET4.0E; MALC)";
        httpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
        httpWebRequest.Timeout = 30000;
        CookieContainer cookieContainer = new CookieContainer();
        HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
        foreach (Cookie c in response.Cookies)
        {
            cookieContainer.Add(c);
        }
        return cookieContainer;
    }
    public bool Login(string Email, string Password, CookieContainer Cookies)
    {
        string s = "handle=" + Email + "&password=" + Password;
        byte[] bytes = Encoding.ASCII.GetBytes(s);
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://site.com/auth/login");
        httpWebRequest.Method = "POST";
        httpWebRequest.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
        httpWebRequest.Referer = "http://www.lockerz.com/";
        httpWebRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, "ru-RU");
        httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; WebMoney Advisor; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Tablet PC 2.0; .NET4.0C; .NET CLR 1.1.4322; .NET4.0E; MALC)";
        httpWebRequest.ContentType = "application/x-www-form-urlencoded";
        httpWebRequest.CookieContainer = Cookies;
        httpWebRequest.ContentLength = (long)bytes.Length;
        httpWebRequest.Headers.Add(HttpRequestHeader.Pragma, "no-cache");
        httpWebRequest.Timeout = 30000;
        httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip;
        Stream requestStream = httpWebRequest.GetRequestStream();
        requestStream.Write(bytes, 0, bytes.Length);
        requestStream.Close();
        HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream());
        string document = reader.ReadToEnd().Trim();
        if (document.IndexOf("Sign Out") > 1)
        {
            httpWebResponse.Close();
            return true;
        }
        else
        {
            httpWebResponse.Close();
            return false;
        }
    }

怎么了? 提前致谢。

【问题讨论】:

  • 不清楚你如何使用GetCookies,但有可能第二个响应只返回一个cookie - 你在请求之间使用相同的CookieContainer,还是重新创建是吗?
  • 我像CookieContainer cookies = GetCookies(); if(!Login(email, password, cookies)){<...>}一样使用它

标签: c# cookies httpwebrequest


【解决方案1】:

在 httpWebRequest 对象中有一个 CookieCollection。如果您深入研究变量,您会发现其中一个 cookie 设置为与您在 URI 中发送的不同的域。您需要通过修改域以匹配 URI 来操作此 cookie。

希望能解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 2015-09-16
    • 2019-05-06
    • 1970-01-01
    相关资源
    最近更新 更多