Posting login data with HttpWebRequest

A

adwooley2

Hello. Have been losing plenty of hair over problem whereby I can't
make it off the login page. Trying to pass login info to a login page
and then move on to another page within the site so that I can download
some data, but I keep getting the login page.
Any ideas on this?

Here's the code:
==================================================

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim httpRequest As HttpWebRequest =
CType(WebRequest.Create("http://www.idirect.co.jp/"), HttpWebRequest)
Dim responseReader As StreamReader = New
StreamReader(httpRequest.GetResponse.GetResponseStream)
Dim responseData As String = responseReader.ReadToEnd
responseReader.Close()

Dim viewState As String = ExtractViewState(responseData)
Dim postData As String =
"iDirectLogin_userID=USERID&iDirectLogin_userPass=PASSWORD"

Dim cookies As CookieContainer = New CookieContainer

httpRequest =
CType(WebRequest.Create("http://www.idirect.co.jp/"), HttpWebRequest)
httpRequest.Method = "POST"
httpRequest.KeepAlive = True
httpRequest.Timeout = 120 * 1000
httpRequest.ContentType = "application/x-www-form-urlencoded"
httpRequest.CookieContainer = cookies

Dim requestWriter As StreamWriter = New
StreamWriter(httpRequest.GetRequestStream)
requestWriter.Write(postData)
requestWriter.Close()

httpRequest.GetResponse.Close()

httpRequest =
CType(WebRequest.Create("http://www.idirect.co.jp/search.aspx"),
HttpWebRequest)
httpRequest.CookieContainer = cookies
responseReader = New
StreamReader(httpRequest.GetResponse.GetResponseStream)
' and read the response
responseData = responseReader.ReadToEnd
responseReader.Close()
Response.Write(responseData)

End Sub
==================================

Here's what's in the form:
<input name="iDirectLogin:userID" type="text" id="iDirectLogin_userID"
style="width:100px;WIDTH: 100px" /></td>
<input name="iDirectLogin:userPass" type="password"
id="iDirectLogin_userPass" style="width:100px;WIDTH: 100px" /></td>
<input type="submit" name="iDirectLogin:btnLogIn" value="Log-In"
id="iDirectLogin_btnLogIn" class="button" style="width:80px;" />


Any help would be greatly appreciated.
Regards,

adwooley2
 
J

Jim Wooley

Hello adwooley2,
Hello. Have been losing plenty of hair over problem whereby I can't
make it off the login page. Trying to pass login info to a login page
and then move on to another page within the site so that I can
download
some data, but I keep getting the login page.
Any ideas on this?
Here's the code:
==================================================
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim httpRequest As HttpWebRequest =
CType(WebRequest.Create("http://www.idirect.co.jp/"), HttpWebRequest)
Dim responseReader As StreamReader = New
StreamReader(httpRequest.GetResponse.GetResponseStream)
Dim responseData As String = responseReader.ReadToEnd
responseReader.Close()
Dim viewState As String = ExtractViewState(responseData)
Dim postData As String =
"iDirectLogin_userID=USERID&iDirectLogin_userPass=PASSWORD"
Dim cookies As CookieContainer = New CookieContainer

httpRequest =
CType(WebRequest.Create("http://www.idirect.co.jp/"), HttpWebRequest)
httpRequest.Method = "POST"
httpRequest.KeepAlive = True
httpRequest.Timeout = 120 * 1000
httpRequest.ContentType = "application/x-www-form-urlencoded"
httpRequest.CookieContainer = cookies
Dim requestWriter As StreamWriter = New
StreamWriter(httpRequest.GetRequestStream)
requestWriter.Write(postData)
requestWriter.Close()
httpRequest.GetResponse.Close()

httpRequest =
CType(WebRequest.Create("http://www.idirect.co.jp/search.aspx"),
HttpWebRequest)
httpRequest.CookieContainer = cookies
responseReader = New
StreamReader(httpRequest.GetResponse.GetResponseStream)
' and read the response
responseData = responseReader.ReadToEnd
responseReader.Close()
Response.Write(responseData)
End Sub
==================================
Here's what's in the form:
<input name="iDirectLogin:userID" type="text" id="iDirectLogin_userID"
style="width:100px;WIDTH: 100px" /></td>
<input name="iDirectLogin:userPass" type="password"
id="iDirectLogin_userPass" style="width:100px;WIDTH: 100px" /></td>
<input type="submit" name="iDirectLogin:btnLogIn" value="Log-In"
id="iDirectLogin_btnLogIn" class="button" style="width:80px;" />
Any help would be greatly appreciated. Regards,

It sounds like you are not persisting the principal 's token in the cookies
for the subsequent request. I'm not sure I can help much, but just wanted
to say I love your username ;)

Jim Wooley
http://devauthority.com/blogs/jwooley/default.asp
 
A

adwooley2

Thanks Jim. Not sure where I'll go with this, but appreciate the
reply.
Yes, nice name indeed. I live in Japan currently, and the
pronunciation of my last name always trips up the Japanese.

Rgds,
adwooley2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top