日期:2014-05-17  浏览次数:20493 次

请问在HttpWebRequest中如何模拟一个PostBack事件?谢谢
看到一个网站用Gridview来显示搜索结果,可是分页不是按URL来写的,所以每一次都得按一下页码来显示其他页面上的内容。也就是说,除了第一页的内容,好像没法通过程序来自动发一个HttpWebRequest来抓取搜索结果。我想请问有没有哪位大侠可以指导一下如何通过程序来自动获取所有页面的搜索结果?
这个网站的分页是这样的代码:
<div id="ctl00_ContentPlaceHolder1_AspNetPager1" style="width:100%;text-align:right;">
<a disabled="disabled" style="margin-right:5px;">上一页</a><span style="margin-right:5px;font-weight:Bold;color:red;">[1]</span>
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AspNetPager1','2')" style="margin-right:5px;">[2]</a>
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AspNetPager1','3')" style="margin-right:5px;">[3]</a>
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AspNetPager1','4')" style="margin-right:5px;">[4]</a>
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AspNetPager1','5')" style="margin-right:5px;">[5]</a>
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AspNetPager1','6')" style="margin-right:5px;">[6]</a>
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AspNetPager1','7')" style="margin-right:5px;">[7]</a>
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AspNetPager1','8')" style="margin-right:5px;">[8]</a>
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AspNetPager1','9')" style="margin-right:5px;">[9]</a>
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AspNetPager1','10')" style="margin-right:5px;">[10]</a>
<span style="margin-right:5px;"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AspNetPager1','11')">...</a></span>
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AspNetPager1','2')" style="margin-right:5px;">下一页</a>
</div>

------解决方案--------------------
注意根据注释里说明做下修改:
VB code

Dim postData As New NameValueCollection
postData.Item("__VIEWSTATE") = "。。。。。。非常长的一个viewstate" ' 把__VIEWSTATE的值复制到这里
postData.Item("__EVENTTARGET") = "ctl00$ContentPlaceHolder1$AspNetPager1"
postData.Item("__EVENTARGUMENT") = "3"

Dim wc As New WebClient()
Dim resp As Byte() = wc.UploadValues("http://url", postData) ' url填要抓取页面的url

Dim strResult As String = New StreamReader(New MemoryStream(resp), True).ReadToEnd()