如何使用 C# 发布到 ASP.NET 验证所需的页面并读取响应

发布于 2025-01-02 12:33:50 字数 2019 浏览 0 评论 0原文

我正在编写自己的特定产品爬虫。现在有一个产品销售网站,其页面使用帖子数据。我真的非常需要能够发布数据并阅读响应。但他们使用的是 asp.net 验证,而且非常混乱。我真的不知道如何正确发布数据和阅读。我正在使用 htmlagilitypack。如果可以使用 htmlagilitypack 发布数据并读取响应,那就太棒了。

现在这是示例页面: http://www.hizlial.com/HizliListele.aspx? CatID=482643

当您打开页面时,查看“urun_listele”类,

您将看到那里的选项

20 Ürün Listele
40 Ürün Listele
60 Ürün Listele
Tümünü Listele

这些数字是要显示的产品数量。 Tümünü listele 意思是列出所有产品。现在我真的需要发布数据并获取该产品类别下的所有产品。我使用 firebug 进行调试并尝试在下面编写代码,但我仍然获得默认数量的产品

        private void button11_Click(object sender, RoutedEventArgs e)
    {
        StringBuilder srBuilder = new StringBuilder();
        AppendPostParameter(srBuilder, "ctl00$ContentPlaceHolder1$cmbUrunSayi", "full");    
        srBuilder = srBuilder.Replace("&", "", srBuilder.Length - 1, 1);
        byte[] byteArray = Encoding.UTF8.GetBytes(srBuilder.ToString());
        HttpWebRequest hWebReq = (HttpWebRequest)WebRequest.Create("http://www.hizlial.com/HizliListele.aspx?CatID=482643");
        hWebReq.Method = "POST";
        hWebReq.ContentType = "application/x-www-form-urlencoded";

        using (Stream requestStream = hWebReq.GetRequestStream())
        {
            requestStream.Write(byteArray, 0, byteArray.Length);
        }
        HtmlDocument hd = new HtmlDocument();

        using (HttpWebResponse response = (HttpWebResponse)hWebReq.GetResponse())
        {
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {              
                var htmlstring = sr.ReadToEnd();
            }
        }
    }

    static private void AppendPostParameter(StringBuilder sb, string name, string value)
    {
        sb.AppendFormat("{0}={1}&", name, HttpUtility.UrlEncode(value));
    }

在获得数据后,我将其加载到 htmlagilitypack HtmlDocument

任何帮助都将受到赞赏。

C# 4.0、wpf 应用程序、htmlagiltiypack

I am writing my own specific product crawler. Now there is a product selling website which uses post data for pages. I really really need to able to post data and read the response. But they are using asp.net validation and it is so messed up. I really could not figure how to properly post data and read. I am using htmlagilitypack. If it is possible to post data with htmlagilitypack and read the response it would be really really awesome.

Now this is the example page : http://www.hizlial.com/HizliListele.aspx?CatID=482643

When you opened the page look at the class "urun_listele"

You will see the options there

20 Ürün Listele
40 Ürün Listele
60 Ürün Listele
Tümünü Listele

Those numbers are product counts to be displayed. Tümünü listele means list all products. Now I really need to post data and get all of the products under that product category. I used firebug to debug and tried to code below but i still got default number of products

        private void button11_Click(object sender, RoutedEventArgs e)
    {
        StringBuilder srBuilder = new StringBuilder();
        AppendPostParameter(srBuilder, "ctl00$ContentPlaceHolder1$cmbUrunSayi", "full");    
        srBuilder = srBuilder.Replace("&", "", srBuilder.Length - 1, 1);
        byte[] byteArray = Encoding.UTF8.GetBytes(srBuilder.ToString());
        HttpWebRequest hWebReq = (HttpWebRequest)WebRequest.Create("http://www.hizlial.com/HizliListele.aspx?CatID=482643");
        hWebReq.Method = "POST";
        hWebReq.ContentType = "application/x-www-form-urlencoded";

        using (Stream requestStream = hWebReq.GetRequestStream())
        {
            requestStream.Write(byteArray, 0, byteArray.Length);
        }
        HtmlDocument hd = new HtmlDocument();

        using (HttpWebResponse response = (HttpWebResponse)hWebReq.GetResponse())
        {
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {              
                var htmlstring = sr.ReadToEnd();
            }
        }
    }

    static private void AppendPostParameter(StringBuilder sb, string name, string value)
    {
        sb.AppendFormat("{0}={1}&", name, HttpUtility.UrlEncode(value));
    }

After i get the data I will load it to the htmlagilitypack HtmlDocument

Any help is appreciated.

C# 4.0 , wpf application, htmlagiltiypack

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‘画卷フ 2025-01-09 12:33:50

ASP .Net 使用 __EVENTTARGET__EVENTARGUMENT 字段来模拟 Windows 窗体行为。要模拟服务器上组合框的更改事件,您需要附加到表单字段以请求它们为 __EVENTTARGET 作为 'ctl00$ContentPlaceHolder1$cmbUrunSayi' 和 __EVENTARGUMENT 为 ''。

如果您查看组合和 __doPostBack 方法的 onchange 代码,您就会明白我的意思。您可以在 srBuilder 声明后插入以下代码。这样代码就可以工作了。

AppendPostParameter(srBuilder,  "__EVENTTARGET", "ctl00$ContentPlaceHolder1$cmbUrunSayi"); 
AppendPostParameter(srBuilder, "__EVENTARGUMENT", string.Empty); 

您还需要提取 __VIEWSTATE & __EVENTVALIDATION 值。要获得它们,只需发送一个虚拟请求并从该请求中提取值和 cookie,然后将它们附加到新请求中......

ASP .Net uses __EVENTTARGET and __EVENTARGUMENT fields to simulate Windows Forms behavior. To simulate Change event of combobox on server you need to append to form field to request they are __EVENTTARGET as 'ctl00$ContentPlaceHolder1$cmbUrunSayi' and __EVENTARGUMENT as ''.

If you look onchange code of combo and __doPostBack method you will understand what I mean. You can insert the code below after your declaration of srBuilder. That way code will work.

AppendPostParameter(srBuilder,  "__EVENTTARGET", "ctl00$ContentPlaceHolder1$cmbUrunSayi"); 
AppendPostParameter(srBuilder, "__EVENTARGUMENT", string.Empty); 

You will also need to extract __VIEWSTATE & __EVENTVALIDATION values. To get them just send a dummy request and extaract that values and cookies from that request and then append them into new one...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文