0

Web クライアントまたは httpwebrequest を使用して Web サイトにログインしたいのですが、ログイン後にサイト内のページに移動したいと考えています。
だから私はデータを投稿してログインする方法を知っています、私の問題はログイン後のナビゲーションにあります.
ログインページにリダイレクトされます.Cookieと関係があることは知っていますが、何をしているのかわかりません. ..ここに私のコードがあります->それはstackoverflowの別の回答から取られています...(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

任意の提案>私が間違っていること

4

1 に答える 1

0

リクエストごとに CookieContainer をインスタンス化しないでください。すべてのリクエストに対して 1 つの CookieContainer が必要です。これを試して:

    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
于 2012-10-08T09:46:50.033 に答える