HTTPWEBRESPONSE and HTTPWEBREQUEST and redirect
Hi All,
I have to post a request to a login page using the userid and password and then get the location and session cookiew from the responsestream, and then in return use that information to redirect the user to the URl that Header 'Location' has.
My question is I can send a get using the responsestream and get the response for the location page but all the images on the site are not showing up. Also If I want to redirect the user to the menu page after login, my session id is lost. I am attaching the code below for both - if someone has a solution to this it will be much appreciated.
Dim dByte() As Byte = enc.GetBytes(sFormdata)
Try
Dim req As HttpWebRequest = HttpWebRequest.Create("https://somesite.com/login")
With req
.KeepAlive = False
.Method = "POST"
.AllowAutoRedirect = False
.ContentType = "application/x-www-form-urlencoded"
.ContentLength = dByte.Length
.CookieContainer = cookieC
End With
'add the POST data
Dim sendREQ As IO.Stream = req.GetRequestStream()
sendREQ.Write(dByte, 0, dByte.Length)
sendREQ.Close()
req.Credentials = CredentialCache.DefaultCredentials
'Obtain the response
Dim Res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
Dim sr As StreamReader = New StreamReader(Res.GetResponseStream(), Text.Encoding.UTF8)
Dim strData As String = sr.ReadToEnd
'Add the headers
Dim redirect As String = Res.Headers("Location")
'add cookie
cookieC.Add(Res.Cookies)
'Move to redirect page as get request
req = WebRequest.Create(redirect)
With req
.KeepAlive = False
.Method = "GET"
.ContentType = "application/x-www-form-urlencoded"
.AllowAutoRedirect = False
.CookieContainer = cookieC
End With
'get the page
Res = req.GetResponse
Dim sReader As IO.StreamReader = New IO.StreamReader(Res.GetResponseStream)
Dim html As String = sReader.ReadToEnd
Response.Write(html)
for the redirect - I was trying to use the code below and instead of going to the menu page I was thrown back to the login page
Dim dByte() As Byte = enc.GetBytes(sFormdata)
Try
Dim req As HttpWebRequest = HttpWebRequest.Create("https://somesite.com/login")
With req
.KeepAlive = False
.Method = "POST"
.AllowAutoRedirect = False
.ContentType = "application/x-www-form-urlencoded"
.ContentLength = dByte.Length
.CookieContainer = cookieC
End With
'add the POST data
Dim sendREQ As IO.Stream = req.GetRequestStream()
sendREQ.Write(dByte, 0, dByte.Length)
sendREQ.Close()
req.Credentials = CredentialCache.DefaultCredentials
'Obtain the response
Dim Res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
Dim sr As StreamReader = New StreamReader(Res.GetResponseStream(), Text.Encoding.UTF8)
Dim strData As String = sr.ReadToEnd
'Add the headers
Dim redirect As String = Res.Headers("Location")
'add cookie
cookieC.Add(Res.Cookies)
Dim rs As HttpResponse = Context.Response
Dim hCookie As New HttpCookie("CHUBB")
hCookie.Name = "Cookie"
hCookie.Value = Res.Cookies(0).Name & "=" & Res.Cookies(0).Value
hCookie.Domain = Res.Cookies(0).Domain
hCookie.Path = Res.Cookies(0).Path
rs.Cookies.Add(hCookie)
rs.Redirect(redirect)
rs.Close()
|