mushimushi84
Member
- Joined
- Jan 22, 2010
- Messages
- 10
- Programming Experience
- 1-3
Ok so ive been looking everywhere to try and bypass a websites "enter" screen using the POST method of webrequest(). I've successfully done this and recieve the HTML of the page after the login page. Problem is now i want to automatically do another webrequest on this page entering GET paramaters equal to doing a 'search' on the page. I figure i need cookies for this to happen which i think im doing correctly, but the second web request just boots me back to the original login screen... does this imply the cookies are not being sent, or they are wrong somehow?? I've been trying to get this to work all day... heres my code so far...
' Set the initial parameters
Dim domain As String = "......."
Dim encoding As New System.Text.ASCIIEncoding
Dim CookieC As New Net.CookieContainer
' Use the appropriate HTML field names to stuff into the post header
Dim PostData As String = "user_choice=Enter(or whatever postdata)"
Dim Data() As Byte = encoding.GetBytes(PostData)
' Initialise the request
Dim LoginReq As Net.HttpWebRequest = Net.WebRequest.Create(domain) ' Login location taken from the form action
With LoginReq
.KeepAlive = False
.Method = "POST"
' Note: if the page uses a redirect if will fail
.AllowAutoRedirect = False
.ContentType = "application/x-www-form-urlencoded"
.ContentLength = Data.Length
' Set empty container
.CookieContainer = CookieC
End With
' Add the POST data
Dim SendReq As IO.Stream = LoginReq.GetRequestStream
SendReq.Write(Data, 0, Data.Length)
SendReq.Close()
' Obtain the response
Dim LoginRes As Net.HttpWebResponse = LoginReq.GetResponse()
Dim sReader1 As IO.StreamReader = New IO.StreamReader(LoginRes.GetResponseStream)
Dim HTML1 As String = sReader1.ReadToEnd
' Retreive the headers from the request (e.g. the location header)
Dim Headers As WebHeaderCollection = LoginRes.Headers()
Dim Redirect As String = LoginRes.Headers("Set-Cookie")
' Add any returned cookies to the cookie collection
Dim tempCookies As Cookie
For Each tempCookies In LoginRes.Cookies
CookieC.Add(tempCookies)
Next
'CookieC.Add(LoginRes.Cookies)
' Move to the redirected page as a GET request...
LoginReq = Net.WebRequest.Create(domain & Redirect)
With LoginReq
'.KeepAlive = False
'.Method = "GET"
'.ContentType = "application/x-www-form-urlencoded"
.AllowAutoRedirect = False
.CookieContainer = CookieC
End With
' Perform the navigate and output the HTML
LoginRes = LoginReq.GetResponse()
Dim sReader As IO.StreamReader = New IO.StreamReader(LoginRes.GetResponseStream)
Dim HTML As String = sReader.ReadToEnd
Like I said, everything WORKS; I get no errors. BUT the second webrequest is returned with the HTML script of the login page, not the validated page.
I hope this makes sense
any help would be mucho awesome
' Set the initial parameters
Dim domain As String = "......."
Dim encoding As New System.Text.ASCIIEncoding
Dim CookieC As New Net.CookieContainer
' Use the appropriate HTML field names to stuff into the post header
Dim PostData As String = "user_choice=Enter(or whatever postdata)"
Dim Data() As Byte = encoding.GetBytes(PostData)
' Initialise the request
Dim LoginReq As Net.HttpWebRequest = Net.WebRequest.Create(domain) ' Login location taken from the form action
With LoginReq
.KeepAlive = False
.Method = "POST"
' Note: if the page uses a redirect if will fail
.AllowAutoRedirect = False
.ContentType = "application/x-www-form-urlencoded"
.ContentLength = Data.Length
' Set empty container
.CookieContainer = CookieC
End With
' Add the POST data
Dim SendReq As IO.Stream = LoginReq.GetRequestStream
SendReq.Write(Data, 0, Data.Length)
SendReq.Close()
' Obtain the response
Dim LoginRes As Net.HttpWebResponse = LoginReq.GetResponse()
Dim sReader1 As IO.StreamReader = New IO.StreamReader(LoginRes.GetResponseStream)
Dim HTML1 As String = sReader1.ReadToEnd
' Retreive the headers from the request (e.g. the location header)
Dim Headers As WebHeaderCollection = LoginRes.Headers()
Dim Redirect As String = LoginRes.Headers("Set-Cookie")
' Add any returned cookies to the cookie collection
Dim tempCookies As Cookie
For Each tempCookies In LoginRes.Cookies
CookieC.Add(tempCookies)
Next
'CookieC.Add(LoginRes.Cookies)
' Move to the redirected page as a GET request...
LoginReq = Net.WebRequest.Create(domain & Redirect)
With LoginReq
'.KeepAlive = False
'.Method = "GET"
'.ContentType = "application/x-www-form-urlencoded"
.AllowAutoRedirect = False
.CookieContainer = CookieC
End With
' Perform the navigate and output the HTML
LoginRes = LoginReq.GetResponse()
Dim sReader As IO.StreamReader = New IO.StreamReader(LoginRes.GetResponseStream)
Dim HTML As String = sReader.ReadToEnd
Like I said, everything WORKS; I get no errors. BUT the second webrequest is returned with the HTML script of the login page, not the validated page.
I hope this makes sense
any help would be mucho awesome