Want to stay logged in when WebRequest

mahmood

Member
Joined
Feb 5, 2007
Messages
20
Programming Experience
Beginner
I have a desktop application with a webbrowser control on it.

If I open a web page using Navigate method every thing is OK and I am logged in to a certain website but when I use WebRequest and WebResponse I am logged out.

How can I stay logged in when I use webrequest?
 
It is probably due to cookies, try assigning a new CookieContainer instance to the CookieContainer property before you make the first request, for subsequent requests assign the same CookieContainer instance.
HttpWebRequest.CookieContainer Property (System.Net)
 
Thanks for reply

This is what MS says:
PHP:
Dim instance As HttpWebRequest
Dim value As CookieContainer
instance.CookieContainer = value

Where do I get the value to assing instance.CookieContainer to? (I mean for the first time because one subsequent requests I would get it from HttpWebResponse, I guess)



.
 
Last edited:
You create a new class instance with the New keyword to invoke a constructor. Here's the general idea I tried to transmit in prevous post:
VB.NET:
Dim cc As [B]New [/B]CookieContainer 

dim first as httpwebrequest = *create*
first.CookieContainer = cc
'...
dim second as httpwebrequest = *create*
second.CookieContainer = cc
'''
 
Thanks for the reply

I did that, I created the CookieContainer and assigned it, but I am still logged out which makes sense to me because I think I have to send a value or something to the remote host. These newly created cookies are empty, how is the remote supposed to know who I am then?

As I mentioned if I navigate to the url inside a webbrowser control I am logged it but when reading a page using httpwebrequest I am not.
 
Last edited:
There could be various methods to login with a request, try searching the web for "httpwebrequest login".
 
There could be various methods to login with a request, try searching the web for "httpwebrequest login".

Actually this is where I started from and ended up comming here to ask for help.
I tried every single suggestion out there and not worked. This one looked very promissing, it is supposed to submit a login form but didn't get any results - I mean didn't log in - :
PHP:
           Dim LOGIN_URL As String = "http://xxxx/Signin.aspx"
        Dim MyWebRequest As HttpWebRequest = WebRequest.Create(LOGIN_URL)

        Dim responseReader = New StreamReader(MyWebRequest.GetResponse().GetResponseStream())
        Dim responseData As String = responseReader.ReadToEnd


        responseReader.Close()

        Dim viewState As String = ExtractViewState(responseData)
        Dim postData As String = String.Format("__VIEWSTATE={0}&TxtUsername={1}&TxtPassword={2}&btnLogin=Login", viewState, "MyUsername", "mypass")

        Dim encoding As New System.Text.ASCIIEncoding()
        Dim by As Byte() = encoding.GetBytes(postData)


        Dim cookies As CookieContainer = New CookieContainer()


        MyWebRequest = WebRequest.Create(LOGIN_URL)
        MyWebRequest.Method = "POST"
        MyWebRequest.ContentLength = by.Length
        '  responseReader = New StreamReader(MyWebRequest.GetResponse().GetResponseStream())

        ' responseData = responseReader.ReadToEnd()

        MyWebRequest.ContentType = "application/x-www-form-urlencoded"

        MyWebRequest.CookieContainer = cookies


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


        MyWebRequest.GetResponse().Close()

        MyWebRequest = WebRequest.Create(LOGIN_URL)
        MyWebRequest.CookieContainer = cookies
        responseReader = New StreamReader(MyWebRequest.GetResponse().GetResponseStream())

        responseData = responseReader.ReadToEnd()
        responseReader.Close()
 
Last edited:
Back
Top