2

我已经完成了这段代码来登录、检索和显示网页:

  // login info array
        string postData = "user_name=tler";
        postData += "&user_password=lodvader";
        byte[] data = Encoding.ASCII.GetBytes(postData);

        // web request
        WebRequest req = WebRequest.Create("http://www.lol.com/login.php");
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = data.Length;

        // stream response to string
        Stream newStream = req.GetRequestStream();
        newStream.Write(data, 0, data.Length);
        newStream.Close();
        StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream(), Encoding.GetEncoding("iso-8859-1"));

        string responseString = reader.ReadToEnd();

        // retrieve text within title
        Regex rx = new Regex(@"(?<=<title>).+?(?=</title>)");

        var variable = rx.Matches(responseString);

        // output
        Console.WriteLine(variable[0]);

        Console.ReadLine();

但是,登录后的以下页面是一个 html 重定向,例如:

<meta http-equiv="refresh" content="3; URL="bb.php">

如何点击此链接并检索下一页?

4

4 回答 4

2

只需向 bb.php 文件发送一个新的 WebRequest。确保您使用相同的 CookieContainer,因为我认为 login.php 使用基于 cookie 的会话来记住您。查看HttpWebRequest.CookieContainer属性。这需要您将 WebRequest 转换为 HttpWebRequest。

补充:(无法在评论中编写示例代码。)

我现在只是在不打样的情况下编写代码......

var cookies = new CookieContainer(); 

var firstReq = (HttpWebRequest)WebRequest.Create(".../login.php");
firstReq.CookieContainer = cookies;

var secondReq = (HttpWebRequest)WebRequest.Create(".../bb.php");
secondReq.CookieContainer = cookies
于 2009-03-10T14:53:11.053 回答
2

我已经找到时间来完成它,这里是回复(我试图尽可能清楚):

        // Cookie for our session
        var cookieContainer = new CookieContainer();

        // Encode post variables
        ASCIIEncoding encoding=new ASCIIEncoding();
        byte[] loginDataBytes = encoding.GetBytes("user_name=belaz&user_password=123");

        // Prepare our login HttpWebRequest
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://blabla.fr/verify.php");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.CookieContainer = cookieContainer;
        request.ContentLength = loginDataBytes.Length;

        // Write encoded post variable to the stream
        Stream newStream = request.GetRequestStream();
        newStream.Write(loginDataBytes, 0, loginDataBytes.Length);
        newStream.Close();

        // Retrieve HttpWebResponse
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        // Link the response cookie to the domain
        cookieContainer.Add(new Uri("http://blabla.fr/"),response.Cookies);

        // Prepare our navigate HttpWebRequest, and set his cookie.
        HttpWebRequest requestProfile = (HttpWebRequest)WebRequest.Create("http://blabla.fr/bb.php");
        requestProfile.CookieContainer = cookieContainer;

        // Retrieve HttpWebResponse
        HttpWebResponse responseProfile = (HttpWebResponse)requestProfile.GetResponse();

        // Retrieve stream response and read it to end
        Stream st = responseProfile.GetResponseStream();
        StreamReader sr = new StreamReader(st);
        string buffer = sr.ReadToEnd();
于 2009-03-22T16:53:47.857 回答
0

HttpWebRequest 有一个名为AllowAutoRedirects的属性。将此设置为真。还有一个名为MaximumAutomaticRedirections的属性。将其设置为某个允许的值,以确保遵循所有这些值。

于 2009-03-10T14:56:49.127 回答
0

您不能轻松地做到这一点,因为元标记由客户端读取并执行。

在这种情况下,当您使用 HttpWebRequest 时,请求并不关心文本可能具有的功能。

因此,您需要在 URL 属性 (bb.php) 中对页面进行另一次请求。

-

如果服务器做了重定向,你就不会有问题。

于 2009-03-10T14:58:24.630 回答