HttpWebRequest实现的post方法的一些细节问题.
红色都是变量或则方法.ExtractViewState是一个方法,把返回页面中的_VIEWSTATE值拿出来.
这段代码是在A程序的A页面的一个BUTTON事件中执行的.
而paths这个变量呢是我从数据库里面取出来的B程序的B页面(即B的登陆页面)
而postData这个变量呢是我品成的一个字符串.用来传到服务器.整个过程都执行顺利.
最后responseData这个变量返回的是在B程序中的B页面成功登陆而正确跳转的C页面html信息.
C页面是显示在我的浏览器上面了,但是本身A程序的A页面.却和C页面在同一个页面中,C在上面A在下面.不知道这个是因为什么,非常苦恼啊.求大虾帮忙!
Dim webRequest As HttpWebRequest
webRequest = CType(webRequest.Create(paths), HttpWebRequest)
Dim responseReader As StreamReader
responseReader = New StreamReader(webRequest.GetResponse().GetResponseStream())
Dim responseData As String
responseData = responseReader.ReadToEnd()
responseReader.Close()
'extract the viewstate value and build out POST data
Dim viewState As String = ExtractViewState(responseData)
Dim postData As String = String.Format("__VIEWSTATE={0}&txtLogin={1}&txtPw={2}&btnLogin=Agree and Login", viewState, Me.ViewState("loginID"), Me.ViewState("Password"))
' have a cookie container ready to receive the forms auth cookie
Dim cookies As New CookieContainer
' now post to the login form
webRequest = CType(webRequest.Create(paths), HttpWebRequest)
webRequest.Method = "POST"
webRequest.ContentType = "application/x-www-form-urlencoded"
webRequest.CookieContainer = cookies
'cookies.Add(webRequest.CookieContainer.)
' write the form values into the request message
Dim requestWriter As StreamWriter
requestWriter = New StreamWriter(webRequest.GetRequestStream())
requestWriter.Write(postData)
requestWriter.Close()
responseReader = New StreamReader(webRequest.GetResponse().GetResponseStream())
'and read the response
responseData = responseReader.ReadToEnd()
responseReader.Close()
Response.Write(responseData)
[解决办法]
学习,帮顶!