Home All Groups Group Topic Archive Search About

Posting login data with HttpWebRequest

Author
27 Sep 2006 5:39 AM
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

Author
28 Sep 2006 12:19 AM
Jim Wooley
Hello adwooley2,

Show quoteHide quote
> 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.aspx
Author
28 Sep 2006 2:17 AM
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


Jim Wooley wrote:
Show quoteHide quote
> 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.aspx