积极答复者
请问在HttpWebRequest中如何模拟一个PostBack事件?谢谢

问题
-
请教一下大侠:我想通过程序来自动发一个HttpWebRequest来抓取搜索结果,搜索结果是分页显示的,需要模拟__doPostBack(eventTarget, eventArgument)
我的环境是英文,以下的代码可以“工作”,不过不是正常工作,我是想模拟点击了第3页,可是返回的结果还是第1页,不知是什么原因?
Dim bt() As Byte
Dim para As String
'Here is the parameters that are passed to the page
para = "__EVENTTARGET=ctl00$ContentPlaceHolder1$AspNetPager1&__EVENTARGUMENT=3"
r.Method = "Post"
r.AllowWriteStreamBuffering = True
bt = System.Text.Encoding.ASCII.GetBytes(para)
r.ContentLength = bt.Length
r.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
r.KeepAlive = False
Dim strResult As String
Dim s As Stream = r.GetRequestStream()
s.Write(bt, 0, bt.Length)
s.Close()
Dim wres As WebResponse
Try
wres = r.GetResponse
Dim sr As StreamReader
sr = New StreamReader(wres.GetResponseStream(), True)
strResult = sr.ReadToEnd
Catch ex As Exception
Throw ex
End Try
另外,我把页面的源码贴在这里:
<body>
<form name="aspnetForm" method="post" action="search.aspx?a5000zzzz" onsubmit="javascript:return WebForm_OnSubmit();" id="aspnetForm">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="。。。。。。非常长的一个viewstate" />
</div>
<script type="text/javascript">
// <![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
。。。。。。
我试图传递过去的是__EVENTTARGET和__EVENTARGUMENT
谢谢。
答案
-
我已经在csdn那里得到答案了(http://topic.csdn.net/u/20091208/03/8f80e3dd-e698-406a-b814-c83cc13b5410.html),谢谢。解决方法如下:
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()
- 已标记为答案 Riquel_DongModerator 2009年12月16日 1:50
全部回复
-
你参考下分页那里的每个分页的每个页码的javascript事件 根据其参数来确定 应该怎样传值postback
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~ -
我已经在csdn那里得到答案了(http://topic.csdn.net/u/20091208/03/8f80e3dd-e698-406a-b814-c83cc13b5410.html),谢谢。解决方法如下:
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()
- 已标记为答案 Riquel_DongModerator 2009年12月16日 1:50
-
你好!
关于ASP.NET方面的问题,我建议你可以去ASP.NET 与 AJAX论坛询问,相信你会得到更好的帮助的。同时也非常感谢Raymond版主一直以来的帮助。
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.