我试图使用HttpwebRequest和Httpwebresponse通过POST登录到一个网站,然后一旦通过认证,就可以将其重定向到新站点中的默认页面。我可以做一个responsereader.ReadttoEnd() put,不确定如何获得一个自动重定向。
Dim ccContainer As New CookieContainer()
Dim encoding As New ASCIIEncoding()
Dim strId As String = "username"
Dim strName As String = "password"
Dim postData As String = "UAPMURL=&UAPMURLxx=xx&login=" & strId
postData += ("&password1=" & strName)
Dim data As Byte() = encoding.GetBytes(postData)
' Prepare web request...
Dim myRequest As HttpWebRequest = DirectCast(WebRequest.Create("http://www.LOGINURLHERE.COM/LOGIN.PHP?"), HttpWebRequest)
Dim cc As New CookieContainer
' <<--- This is the key word of the day
myRequest.Method = "POST"
myRequest.AllowAutoRedirect = False
myRequest.ContentType = "application/x-www-form-urlencoded"
myRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
myRequest.KeepAlive = True
myRequest.CookieContainer = New CookieContainer()
myRequest.ContentLength = data.Length
Dim newStream As Stream = myRequest.GetRequestStream()
' Send the data.
newStream.Write(data, 0, data.Length)
newStream.Close()
Dim _response As HttpWebResponse = DirectCast(myRequest.GetResponse(), HttpWebResponse)
If myRequest.HaveResponse Then
For Each retCookie As Cookie In _response.Cookies
cc.Add(retCookie)
Next
End If
Dim request As HttpWebRequest = DirectCast(WebRequest.Create("http://www.DESTINATIONURL.COM/Main.php"), HttpWebRequest)
request.CookieContainer = cc
request.AllowAutoRedirect = False
Dim _res As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)如果我这么做..。
注释掉这一行
'Dim _res As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)使用these...it读取并将来自目标URL的数据填充到当前页面。我是不是漏了什么东西让它自动重定向?谢谢
Dim request As HttpWebRequest = DirectCast(WebRequest.Create("http://www.destinationurl.com"), HttpWebRequest)
request.CookieContainer = cc
request.AllowAutoRedirect = False
Dim responseReader As New StreamReader(request.GetResponse().GetResponseStream())
Dim responseData As String = responseReader.ReadToEnd()
responseReader.Close()
Response.Write(responseData)发布于 2011-10-11 19:44:52
您不重定向;服务器重定向。
如果服务器向您发送重定向响应(
。
https://stackoverflow.com/questions/7731422
复制相似问题