2

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 ?

ykh
  • 1,775
  • 3
  • 31
  • 57
  • What happens if you read the response of the first request before making the second one? – Ravi Y Jan 28 '13 at 06:36
  • The content is retrieved. – ykh Jan 28 '13 at 06:45
  • Is there a reason you're using the `POST` method for the second request, even though you're not appending any data to the end of the request? As an aside, make sure to wrap your `HttpWebResponse` in a `using` statement, or make sure you call `response.Close()` after you're done using the response. – Ichabod Clay Jan 28 '13 at 06:55
  • Sorry that was by mistake, i clearly want GET, but even with get it's the same. I will update my code. – ykh Jan 28 '13 at 07:08

1 Answers1

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

Community
  • 1
  • 1
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
  • i am passing the objCookeContainer for bother requests so when it's first filled it will passed to the second request. Anyhow i tired your code still not working. I am using .Net 4.0 – ykh Jan 28 '13 at 06:50