0

I want to login to a website using a webclient or httpwebrequest, after i'm logged in i want to navigate to a page in the site.
so i know how to post data and to login, my problem is with the navigation after the login,
it redirect me to the login page, i know it has to do with the cookies, but i dont understand what i'm doing worng... here is my code -> it is taken from another answear on stackoverfllow... (http://stackoverflow.com/questions/8425593/c-sharp-httpwebrequest-post-login-to-facebook)

        string getUrl = "somesite.com";
        CookieCollection cookies = new CookieCollection();
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getUrl);
        request.CookieContainer = new CookieContainer();
        request.CookieContainer.Add(cookies);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        cookies = response.Cookies;

        string getUrl2 = "somepage.login.com";
        string postData = String.Format("username={0}&userpassword={1}", "foo", "foofoo");

        HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl2);
        getRequest.CookieContainer = new CookieContainer();
        getRequest.CookieContainer.Add(cookies);

        getRequest.Method = WebRequestMethods.Http.Post;
        getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
        getRequest.AllowWriteStreamBuffering = true;
        getRequest.ProtocolVersion = HttpVersion.Version11;
        getRequest.AllowAutoRedirect = true;
        getRequest.ContentType = "application/x-www-form-urlencoded";

        byte[] byteArray = Encoding.ASCII.GetBytes(postData);
        getRequest.ContentLength = byteArray.Length;
        Stream newStream = getRequest.GetRequestStream(); 
        newStream.Write(byteArray, 0, byteArray.Length); 
        newStream.Close();

        HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
        using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
        {
            string sourceCode = sr.ReadToEnd();
        }
        //untill here it works fine i'm logged in....


        HttpWebRequest getRequest2 = (HttpWebRequest)WebRequest.Create("a page on the site..");
        getRequest2.CookieContainer = new CookieContainer();
        getRequest2.CookieContainer.Add(cookies); 
        HttpWebResponse getResponse2 = (HttpWebResponse)getRequest2.GetResponse();
        cookies = getResponse2.Cookies;

        using (StreamReader sr = new StreamReader(getResponse2.GetResponseStream()))
        {
            string sourceCode = sr.ReadToEnd();
        }
        //here it rediects me to login page

any suggestions> what i'm doing wrong

user1071420
  • 93
  • 3
  • 11

1 Answers1

0

Do not instantiate CookieContainer for each request. You need one CookieContainer over all requests. Try this:

    CookieContainer cc = new CookieContainer();

    string getUrl = "somesite.com";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getUrl);
    request.CookieContainer = cc;
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    string getUrl2 = "somepage.login.com";
    string postData = String.Format("username={0}&userpassword={1}", "foo", "foofoo");

    HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl2);
    getRequest.CookieContainer = cc;

    getRequest.Method = WebRequestMethods.Http.Post;
    getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
    getRequest.AllowWriteStreamBuffering = true;
    getRequest.ProtocolVersion = HttpVersion.Version11;
    getRequest.AllowAutoRedirect = true;
    getRequest.ContentType = "application/x-www-form-urlencoded";

    byte[] byteArray = Encoding.ASCII.GetBytes(postData);
    getRequest.ContentLength = byteArray.Length;
    Stream newStream = getRequest.GetRequestStream(); 
    newStream.Write(byteArray, 0, byteArray.Length); 
    newStream.Close();

    HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
    using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
    {
        string sourceCode = sr.ReadToEnd();
    }
    //untill here it works fine i'm logged in....


    HttpWebRequest getRequest2 = (HttpWebRequest)WebRequest.Create("a page on the site..");
    getRequest2.CookieContainer = cc;
    HttpWebResponse getResponse2 = (HttpWebResponse)getRequest2.GetResponse();

    using (StreamReader sr = new StreamReader(getResponse2.GetResponseStream()))
    {
        string sourceCode = sr.ReadToEnd();
    }
    //here it rediects me to login page
Danilo Vulović
  • 2,983
  • 20
  • 31