C#中http GET方法、计算参数和POST方法之间的网页刷新

本文关键字:方法 之间 POST 刷新 网页 计算 http GET 参数 | 更新日期: 2023-09-27 18:25:24

我有一个页面要解析(抓取),但首先我必须通过控制措施
我设法计算了一些控制号,当我试图通过POST方法参数传递这些控制号和其他内容时,我认为页面会自行刷新并生成新的控制号,所以我计算的控制号不会通过检查,我无法访问所需的页面。

首先,我使用HtmlAgilityPack方法获取页面,并获取这些控制号的值:

HtmlWeb web = new HtmlWeb();
HtmlDocument mainPage = web.Load(url);
int controlNumber = FindControlNumber();

在那之后,我尝试通过POST方法传递计算出的数字:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(newUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string data = @"id=" + id + "&controlNumber=" + controlNumber;
byte[] dataStream = Encoding.UTF8.GetBytes(data);
request.ContentLength = dataStream.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(dataStream, 0, dataStream.Length);
newStream.Close();
HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(webResponse.GetResponseStream());
string html = sr.ReadToEnd();

但是,最初的页面并没有导航到所需的页面,而是显示消息"错误的控制号"。

我做错了什么?

C#中http GET方法、计算参数和POST方法之间的网页刷新

实际上,我唯一需要做的就是设置cookie,并使用WebRequest和WebResponse,而不是HPA的HtmlWeb
以下是有效的代码:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";
        request.KeepAlive = true;
        CookieContainer cookies = new CookieContainer(); // instantiate cookie container
        request.CookieContainer = cookies;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        var stream = response.GetResponseStream();
        // Calculate control number...
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(newUrl);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        string data = @"id=" + id + "&controlNumber=" + controlNumber;
        byte[] dataStream = Encoding.UTF8.GetBytes(data);
        request.ContentLength = dataStream.Length;
        request.CookieContainer = cookies;
        Stream newStream = request.GetRequestStream();
        newStream.Write(dataStream, 0, dataStream.Length);
        newStream.Close();
        HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
        StreamReader sr = new StreamReader(webResponse.GetResponseStream());
        string html = sr.ReadToEnd();