【问题标题】:Passing cookie with HttpWebRequest in winforms?在winforms中使用HttpWebRequest传递cookie?
【发布时间】:2013-01-11 11:22:23
【问题描述】:

请看以下代码:

    objCookieContainer = new CookieContainer();

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://website.com/login.php?user=xxx&pass=xxx");
    request.Method = WebRequestMethods.Http.Get;
    request.Timeout = 15000;
    request.Proxy = null;
    request.CookieContainer = objCookieContainer;

    HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create("http://website.com/page.php?link=url");
    newRequest.Method = WebRequestMethods.Http.Get;
    newRequest.Timeout = 15000;
    newRequest.Proxy = null;
    newRequest.CookieContainer = objCookieContainer;

    HttpWebResponse response = null;

    response = (HttpWebResponse)request.GetResponse();
    string readerRequest = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();

    response = (HttpWebResponse)newRequest.GetResponse();
    string readerNewRequest = new StreamReader(response.GetResponseStream()).ReadToEnd();

使用 request.GetResponse() 后,cookie 已成功填充数据,并且它具有身份验证代码,并且 readerRequest 也已填充,之后我调用 newRequest.GetResponse() 但 readerNewRequest 为空,我尝试做很多事情但结果总是相同,我解决这个问题的唯一方法是使用一个 WebBrowser 对象,我在其中传递 url 并且我能够使用 WebBrowser.DocumentStream 获取内容。

我该如何解决这个问题?

【问题讨论】:

  • 如果在发出第二个请求之前阅读第一个请求的响应会发生什么?
  • 内容检索完毕。
  • 您是否有理由在第二个请求中使用POST 方法,即使您没有在请求末尾附加任何数据?顺便说一句,请确保将您的 HttpWebResponse 包含在 using 语句中,或者确保在使用完响应后调用 response.Close()
  • 对不起,这是错误的,我显然想要 GET,但即使得到它也是一样的。我会更新我的代码。

标签: c# winforms cookies httpwebrequest cookiecontainer


【解决方案1】:
    private void button1_Click(object sender, EventArgs e)
    {
        var objCookieContainer = new CookieContainer();

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://website.com/login.php?user=xxx&pass=xxx");
        request.Method = WebRequestMethods.Http.Get;
        request.Timeout = 15000;
        request.Proxy = null;
        request.CookieContainer = objCookieContainer;

        HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create("http://website.com/page.php?link=url");
        newRequest.Method = WebRequestMethods.Http.Post;
        newRequest.Timeout = 15000;
        newRequest.Proxy = null;


        HttpWebResponse response = null;

        response = (HttpWebResponse)request.GetResponse();
        //once you read response u need to add all cookie sent in header to the 'objCookieContainer' so that it can be forwarded on second response
        foreach (Cookie cookie in response.Cookies)
        {
            objCookieContainer.Add(cookie);
        }
        string readerRequest = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();


        //since you have added the cookies, this must response fine now
        newRequest.CookieContainer = objCookieContainer;
        response = (HttpWebResponse)newRequest.GetResponse();
        string readerNewRequest = new StreamReader(response.GetResponseStream()).ReadToEnd();
    }

如果您的 .NET 版本低于 4.0,您可能会遇到 CookieContainer 错误:检查This Link

【讨论】:

  • 我将 objCookeContainer 传递给打扰请求,因此当它第一次填充时,它将传递给第二个请求。无论如何,我厌倦了您的代码仍然无法正常工作。我正在使用.Net 4.0
猜你喜欢
  • 2011-02-27
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多