I'm trying to use HttpwebRequest and Httpwebresponse to log into a website via POST then once authenticated have it redirect to a default page within the new site. I'm able to do a responsereader.ReadttoEnd() put am unsure of how to get an automatic redirect.
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)
If I do this ...
Comment out this line
'Dim _res As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
Use these...it reads and fill my current page with the data from the destination URL. Am I missing something to get it to auto redirect? Thanks
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)