简体   繁体   English

HttpWebRequest-使用__EVENTTARGET和__EVENTARGUMENT发布

[英]HttpWebRequest - Post with __EVENTTARGET and __EVENTARGUMENT

I want to read all content from a website that is using Asp.net GridView contorl with pagination. 我想从使用Asp.net GridView控制和分页的网站上读取所有内容。 Now, pagination is done with POST method by Asp.Net grid using __dopostback with required parameters. 现在,使用__dopostback和必需的参数,通过Asp.Net网格的POST方法完成分页。

I tried to read pagination data using HttpWebRequest with following code but it always throws "500 internal server error." 我尝试使用带有以下代码的HttpWebRequest读取分页数据,但它始终抛出“ 500内部服务器错误”。

  public string GetPageData(int page)
    {
        var request = (HttpWebRequest)WebRequest.Create("http://localhost:61141/Default.aspx");
        string postData = "__EVENTTARGET=ctl00$MainContent$GridView1&__EVENTARGUMENT=Page$"+ page;


        request.Method = "POST";
        var bt = System.Text.Encoding.ASCII.GetBytes(postData);
        request.ContentLength = bt.Length;
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = " Mozilla/4.0 (compatible; MSIE 6; Windows NT 5) ";
        request.KeepAlive = false;
        request.AllowWriteStreamBuffering = true;

        string strResult;
        Stream stream = request.GetRequestStream();
        stream.Write(bt, 0, bt.Length);
        stream.Close();

        var response = request.GetResponse();
        var sr = new StreamReader(response.GetResponseStream(), true);
        strResult = sr.ReadToEnd();

        return strResult;
    }

Please suggest any workaround over it. 请提出任何解决方法。 Thanks in advanced. 提前致谢。

WebForms pagination relies on JavaScript so is intended to be performed within the web browser, not via a custom call as you have here. WebForms分页依赖于JavaScript,因此旨在在Web浏览器中执行,而不像您在此处那样通过自定义调用执行。 Why not set up a Web API endpoint or other REST-enabled endpoint in the web application? 为什么不在Web应用程序中设置Web API端点或其他启用REST的端点? This has the added benefit that you can return the results as XML or JSON, which removes the need for screen scraping and makes client side processing much easier. 这具有额外的好处,您可以将结果以XML或JSON的形式返回,从而消除了屏幕抓取的需要,并使客户端处理变得更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM