I am trying to use HTTPWebRequest to login to a site and then retrieve the page after login. However, it seems as if I can't get past the login. I also investigated with Fiddler and tried mimicking Fiddler almost completely and still no luck.
Any idea what I am doing wrong?
Thanks,
Mohammed
Dim webRequest As HttpWebRequest
Dim responseReader As StreamReader
Dim responseData As String
Dim postData As String = "login=testexpert@yahoo.com&password=testexpert"
Dim cookies As CookieContainer = New CookieContainer()
Dim requestWriter As StreamWriter
Try
'post form data to page
strUrl = "https://www.ideeli.com/login"
webRequest = HttpWebRequest.Create(strUrl)
webRequest.Method = WebRequestMethods.Http.Post
webRequest.ContentType = "application/x-www-form-urlencoded"
webRequest.CookieContainer = cookies
webRequest.ContentLength = postData.Length
requestWriter = New StreamWriter(webRequest.GetRequestStream)
requestWriter.Write(postData)
requestWriter.Close()
'recieve cookie
webRequest.GetResponse().Close()
'now we send the cookie
webRequest = HttpWebRequest.Create("http://www.ideeli.com")
webRequest.CookieContainer = cookies
responseReader = New StreamReader(webRequest.GetResponse.GetResponseStream())
responseData = responseReader.ReadToEnd()
responseReader.Close()
strRequestedHTML = responseData
Return
Catch ex As Exception
ErrorLabel.Text += "<br />There was an error going to this site: " + strUrl + "<br> Error: " + ex.Message
Return
End Try
You are directly posting the data, I dont advice that
Do this:
1. Get the Login Page
2. You will be returned cookies with it, save em
3. Post your Username, Pass, Cookies (now=1283536729; bonus=f9bf23fa-5c15-4549-be29-ec40fd94aa7f; production_session_id=6463d688199f4fd848b5417d25ca3add; ignore_browser_check=on) ...
I also found three more vars like remember me, x and y for the site you described, you should send them too.
Also dont forget to set the referrer to the login page URL
Stop messing with Fiddler, try Tamper Data (its a Firefox Addon, works everytime...) And I would also advice you to use Wireshark or similar thing, see the packets themselves.
See Ya!
Marked as answer by aatif786 on Sep 04, 2010 04:15 AM
Great that algorithm solved it. The new code is below. Thank you so much!
Dim webRequest As HttpWebRequest
Dim responseReader As StreamReader
Dim responseData As String
Dim postData As String = "login=testexpert@yahoo.com&password=testexpert"
Dim cookies As CookieContainer = New CookieContainer()
Dim requestWriter As StreamWriter
Try
'get login page with cookies
strUrl = "https://www.ideeli.com/login"
webRequest = HttpWebRequest.Create(strUrl)
webRequest.CookieContainer = cookies
'recieve non-authenticated cookie
webRequest.GetResponse().Close()
'post form data to page
strUrl = "https://www.ideeli.com/login"
webRequest = HttpWebRequest.Create(strUrl)
webRequest.Method = WebRequestMethods.Http.Post
webRequest.ContentType = "application/x-www-form-urlencoded"
webRequest.CookieContainer = cookies
webRequest.ContentLength = postData.Length
requestWriter = New StreamWriter(webRequest.GetRequestStream)
requestWriter.Write(postData)
requestWriter.Close()
'recieve authenticated cookie
webRequest.GetResponse().Close()
'now we get the authenticated page
webRequest = HttpWebRequest.Create("http://www.ideeli.com")
webRequest.CookieContainer = cookies
responseReader = New StreamReader(webRequest.GetResponse.GetResponseStream())
responseData = responseReader.ReadToEnd()
responseReader.Close()
strRequestedHTML = responseData
Catch ex As Exception
ErrorLabel.Text += "<br />There was an error going to this site: " + strUrl + "<br> Error: " + ex.Message
Return
End Try
HttpWebRequest cookie
Marked as answer by aatif786 on Sep 04, 2010 04:21 AM
aatif786
Member
1 Points
2 Posts
HttpWebRequest Form Authentication Not Getting Past Login Page
Sep 03, 2010 08:29 AM|LINK
Hey Guys,
I am trying to use HTTPWebRequest to login to a site and then retrieve the page after login. However, it seems as if I can't get past the login. I also investigated with Fiddler and tried mimicking Fiddler almost completely and still no luck.
Any idea what I am doing wrong?
Thanks,
Mohammed
Dim webRequest As HttpWebRequest Dim responseReader As StreamReader Dim responseData As String Dim postData As String = "login=testexpert@yahoo.com&password=testexpert" Dim cookies As CookieContainer = New CookieContainer() Dim requestWriter As StreamWriter Try 'post form data to page strUrl = "https://www.ideeli.com/login" webRequest = HttpWebRequest.Create(strUrl) webRequest.Method = WebRequestMethods.Http.Post webRequest.ContentType = "application/x-www-form-urlencoded" webRequest.CookieContainer = cookies webRequest.ContentLength = postData.Length requestWriter = New StreamWriter(webRequest.GetRequestStream) requestWriter.Write(postData) requestWriter.Close() 'recieve cookie webRequest.GetResponse().Close() 'now we send the cookie webRequest = HttpWebRequest.Create("http://www.ideeli.com") webRequest.CookieContainer = cookies responseReader = New StreamReader(webRequest.GetResponse.GetResponseStream()) responseData = responseReader.ReadToEnd() responseReader.Close() strRequestedHTML = responseData Return Catch ex As Exception ErrorLabel.Text += "<br />There was an error going to this site: " + strUrl + "<br> Error: " + ex.Message Return End TryHttpWebReques login
CandorZ
Participant
1346 Points
275 Posts
Re: HttpWebRequest Form Authentication Not Getting Past Login Page
Sep 03, 2010 06:22 PM|LINK
You are directly posting the data, I dont advice that
Do this:
1. Get the Login Page
2. You will be returned cookies with it, save em
3. Post your Username, Pass, Cookies (now=1283536729; bonus=f9bf23fa-5c15-4549-be29-ec40fd94aa7f; production_session_id=6463d688199f4fd848b5417d25ca3add; ignore_browser_check=on) ...
I also found three more vars like remember me, x and y for the site you described, you should send them too.
Also dont forget to set the referrer to the login page URL
Stop messing with Fiddler, try Tamper Data (its a Firefox Addon, works everytime...) And I would also advice you to use Wireshark or similar thing, see the packets themselves.
See Ya!
aatif786
Member
1 Points
2 Posts
Re: HttpWebRequest Form Authentication Not Getting Past Login Page
Sep 04, 2010 04:20 AM|LINK
Great that algorithm solved it. The new code is below. Thank you so much!
Dim webRequest As HttpWebRequest Dim responseReader As StreamReader Dim responseData As String Dim postData As String = "login=testexpert@yahoo.com&password=testexpert" Dim cookies As CookieContainer = New CookieContainer() Dim requestWriter As StreamWriter Try 'get login page with cookies strUrl = "https://www.ideeli.com/login" webRequest = HttpWebRequest.Create(strUrl) webRequest.CookieContainer = cookies 'recieve non-authenticated cookie webRequest.GetResponse().Close() 'post form data to page strUrl = "https://www.ideeli.com/login" webRequest = HttpWebRequest.Create(strUrl) webRequest.Method = WebRequestMethods.Http.Post webRequest.ContentType = "application/x-www-form-urlencoded" webRequest.CookieContainer = cookies webRequest.ContentLength = postData.Length requestWriter = New StreamWriter(webRequest.GetRequestStream) requestWriter.Write(postData) requestWriter.Close() 'recieve authenticated cookie webRequest.GetResponse().Close() 'now we get the authenticated page webRequest = HttpWebRequest.Create("http://www.ideeli.com") webRequest.CookieContainer = cookies responseReader = New StreamReader(webRequest.GetResponse.GetResponseStream()) responseData = responseReader.ReadToEnd() responseReader.Close() strRequestedHTML = responseData Catch ex As Exception ErrorLabel.Text += "<br />There was an error going to this site: " + strUrl + "<br> Error: " + ex.Message Return End TryHttpWebRequest cookie