如何使用HttpWebRequest登录网站

时间:2011-11-29 10:15:48

标签: .net c#-4.0 httpwebrequest

有人可以帮我弄清楚如何使用HttpWebRequest登录页面并随后刮取页面。使用的代码并不只是在登录页面上写出标记但无法登录...我尝试登录的网站是基于php的网站。

        // first, request the login form to get the viewstate value
        HttpWebRequest webRequest = WebRequest.Create("loginPageUrl") as HttpWebRequest;
        StreamReader responseReader = new StreamReader(
              webRequest.GetResponse().GetResponseStream()
           );
        string responseData = responseReader.ReadToEnd();
        responseReader.Close();


        string postData = String.Format("Username={0}&Password={1}", "user", "pwd");

        // have a cookie container ready to receive the forms auth cookie
        CookieContainer cookies = new CookieContainer();

        // now post to the login form
        webRequest = WebRequest.Create("loginPostUrl") as HttpWebRequest;
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.CookieContainer = cookies;

        // write the form values into the request message
        StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
        requestWriter.Write(postData);
        requestWriter.Close();

        // we don't need the contents of the response, just the cookie it issues
        webRequest.GetResponse().Close();

        // now we can send out cookie along with a request for the protected page
        webRequest = WebRequest.Create("PageToScrapeUrl") as HttpWebRequest;
        webRequest.CookieContainer = cookies;
        responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());

        // and read the response
        responseData = responseReader.ReadToEnd();
        responseReader.Close();

        Console.WriteLine(responseData);
        Console.ReadKey();

1 个答案:

答案 0 :(得分:0)

使用WireShark等工具捕获实际的浏览器流量,查看它发送的内容,然后返回。然后在您的代码中复制行为。你的代码的基础是正确的,你只需要调整它。