Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 687
  • Last Modified:

HttpWebRequest Form Authentication Not Getting Past Login Page

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 Try

Open in new window

0
mohali786
Asked:
mohali786
  • 2
1 Solution
 
ZhaolaiCommented:
Tried your code. Here is the return page:
Is it what you expected?
Do you get any errors? If so, what errors?


Screenshot.PNG
0
 
mohali786Author Commented:
I get the same page. However, this is not the page that I want. If I tried going to Ideeli.com without being logged in I would get this page. If I tried the same while being logged in I would get the sales page. I want to see the sales page.
0
 
mohali786Author Commented:
Here is the code to resolve that. Basically what I do different is to GET the non-authenticated cookie first and then POST that with my login information
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

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now