如何发布到asp.net验证所需的页面,C#和读取响应 [英] How to post to asp.net validation required page with C# and read response

查看:88
本文介绍了如何发布到asp.net验证所需的页面,C#和读取响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写我自己的特定产品履带。现在有一个产品销售网站,该网站使用POST数据页。我真的真的需要能够发布数据和读取响应。但他们使用asp.net验证,它是那么糟糕。我实在想不出如何正确发布数据和读取。我使用htmlagilitypack。如果有可能与htmlagilitypack发布的数据和读取响应这将是真的真棒。

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.

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

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

当你打开网页看类urun_listele

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

您将看到的选项有

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

这些数字被显示产品计数。 Tümünülistele意味着列表中的所有产品。现在,我真的需要发布的数据,让所有的产品下的产品类别。我用萤火虫来调试,并试图跌破code,但我仍然有产品的默认数量

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));
    }

我得到的数据后,我将其加载到htmlagilitypack的HTMLDocument

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

任何帮助是AP preciated。

Any help is appreciated.

C#4.0,WPF应用程序,htmlagiltiypack

C# 4.0 , wpf application, htmlagiltiypack

推荐答案

ASP .NET使用 __ EVENTTARGET __ EVENTARGUMENT 场模拟Windows窗体的行为。为了模拟组合框的Change事件上,你需要追加到表单字段,要求它们 __ EVENTTARGET 为ctl00 $ ContentPlaceHolder1 $ cmbUrunSayi和 __ EVENTARGUMENT <服务器/ code>为''。

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 ''.

如果你看连击的onchange code和__doPostBack的方法,你明白我的意思。您可以将您的srBuilder声明之后插入下面的code。这样,code会工作。

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); 

您还需要提取__VIEWSTATE&安培; __EVENTVALIDATION值。为了让他们只需发送一个虚拟的请求,并extaract从该请求,然后价值观和饼干追加它们变成新的......

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...

这篇关于如何发布到asp.net验证所需的页面,C#和读取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆