传递的cookie的HttpWebRequest中的WinForms? [英] Passing cookie with HttpWebRequest in winforms?

查看:143
本文介绍了传递的cookie的HttpWebRequest中的WinForms?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅下面的代码:

  objCookieContainer =新的CookieContainer(); 

HttpWebRequest的要求=(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响应= NULL;

响应=(HttpWebResponse)request.GetResponse();
串readerRequest =新的StreamReader(response.GetResponseStream(),Encoding.UTF8).ReadToEnd();

响应=(HttpWebResponse)newRequest.GetResponse();
串readerNewRequest =新的StreamReader(response.GetResponseStream())为ReadToEnd()。



在使用request.GetResponse(),cookie将被成功地用数据填充它有它的后验证码和readerRequest填充过,在那之后我称之为newRequest.GetResponse(),但readerNewRequest是空的,我试着做很多事情,但总是相同的结果,只有这样,我已经解决了,这是通过在web浏览器对象我通过网址,我是能够得到使用WebBrowser.DocumentStream的内容。



我怎样才能解决呢?

解决方案

 私人无效的button1_Click(对象发件人,EventArgs五)
{
变种objCookieContainer =新的CookieContainer() ;

HttpWebRequest的要求=(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响应= NULL;

响应=(HttpWebResponse)request.GetResponse();
//一旦你读响应u需要添加的头部发送到objCookieContainer所有的cookie,以便它可以在第二个响应
的foreach被转发(在response.Cookies饼干饼干)
{
objCookieContainer.Add(饼干);
}
串readerRequest =新的StreamReader(response.GetResponseStream(),Encoding.UTF8).ReadToEnd();


//既然你已经添加了饼干,这必须应对现在罚款
newRequest.CookieContainer = objCookieContainer;
响应=(HttpWebResponse)newRequest.GetResponse();
串readerNewRequest =新的StreamReader(response.GetResponseStream())为ReadToEnd()。
}






如果你是.NET版本低于4.0ü可能会遇到的CookieContainer错误:检查此链接


Please see the following code:

    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();

After the using the request.GetResponse(), the cookie is populated with data successfully and it have it's authentication code and the readerRequest is populated too, after that i call the newRequest.GetResponse() but the readerNewRequest is empty, i tried to do many things but always same result, the only way i have solved this is by using a WebBrowser object in which i pass the url and i was able to get the content using the WebBrowser.DocumentStream.

How can i solve this ?

解决方案

    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();
    }


if you are .NET version below 4.0 u may encounter a CookieContainer Bug : Check This Link

这篇关于传递的cookie的HttpWebRequest中的WinForms?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆