Passing cookie with HttpWebRequest in winforms?

2019-02-19 12:54发布

问题:

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 ?

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

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