Solved

HttpWebRequest Form Authentication Not Getting Past Login Page

Posted on 2010-09-03
3
661 Views
Last Modified: 2012-08-13
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
Comment
Question by:mohali786
  • 2
3 Comments
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33600771
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
 

Author Comment

by:mohali786
ID: 33600988
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
 

Accepted Solution

by:
mohali786 earned 0 total points
ID: 33601929
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

      Install BugTracker on Windows 2008 server Step 1:  Install windows 2008 server 32 bit OS and configure IIS. Step 2:  Install SQL server ( SQL server 2005 or SQL server 2005 Express edition. The installer for 2008  version isn’t very f…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now