Home All Groups Group Topic Archive Search About

How to use the httpwebrequest with Cookies in "GET" method

Author
26 May 2006 7:47 PM
James MA
I'm now writing a small program to communicate a web server to simulate a web
client.  I use te httpwebrequest to talk with the server, and it works find
for "POST" method, however, when i test other link using "GET" method, i
found that the cookies data has not included in the request.

Here is the sample:


  ' sURL is the URL of server page
  ' pCookies is a varible contain the cookies data
  '
  Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(sURL),
HttpWebRequest)
  myHttpWebRequest.UserAgent = _HttpUserAgent
  myHttpWebRequest.CookieContainer = New System.Net.CookieContainer
  myHttpWebRequest.CookieContainer.Add(pCookies)

  Dim myHttpWebResponse As HttpWebResponse =
CType(myHttpWebRequest.GetResponse(), HttpWebResponse)


I find that the web server cannot obtain the cookies data in this case, and
I have capture the packing from my pc, i find that the packet of "HTTP GET"
does not contain the cookies data.

However, if I change the program to use "POST" method with some dummy data,

  Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(sURL),
HttpWebRequest)
  myHttpWebRequest.UserAgent = _HttpUserAgent
  myHttpWebRequest.CookieContainer = New System.Net.CookieContainer
  myHttpWebRequest.CookieContainer.Add(pCookies)

  Dim sData As String = "a=1"
  myHttpWebRequest.Method = "POST"
  myHttpWebRequest.ContentLength = sData.Length
  myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"

  Dim myStreamWriter As StreamWriter = Nothing
  myStreamWriter = New StreamWriter(myHttpWebRequest.GetRequestStream())
  myStreamWriter.Write(psPostData)
  myStreamWriter.Close()

  Dim myHttpWebResponse As HttpWebResponse =
CType(myHttpWebRequest.GetResponse(), HttpWebResponse)


It works fine, and I capture the packet again, I find that the required
cookie is included in the "HTTP POST" packet.


In fact, I'm not sure how to use the cookies in this case, I just modify the
code from books which does not include cookies, and I combine the example for
using cookies with "POST" method.  So, I'd like to know if I have missed some
step for a "Get" method with cookies.

BTW, may I know if there has any example on using the Cookies with
httpwebrequest using "GET" method?


Thanks in advance!

Author
27 May 2006 6:25 PM
tomb
James MA wrote:

Show quoteHide quote
>I'm now writing a small program to communicate a web server to simulate a web
>client.  I use te httpwebrequest to talk with the server, and it works find
>for "POST" method, however, when i test other link using "GET" method, i
>found that the cookies data has not included in the request.
>
>Here is the sample:
>
>
>  ' sURL is the URL of server page
>  ' pCookies is a varible contain the cookies data
>  '
>  Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(sURL),
>HttpWebRequest)
>  myHttpWebRequest.UserAgent = _HttpUserAgent
>  myHttpWebRequest.CookieContainer = New System.Net.CookieContainer
>  myHttpWebRequest.CookieContainer.Add(pCookies)
>
>  Dim myHttpWebResponse As HttpWebResponse =
>CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
>
>
>I find that the web server cannot obtain the cookies data in this case, and
>I have capture the packing from my pc, i find that the packet of "HTTP GET"
>does not contain the cookies data.
>
>However, if I change the program to use "POST" method with some dummy data,
>
>  Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(sURL),
>HttpWebRequest)
>  myHttpWebRequest.UserAgent = _HttpUserAgent
>  myHttpWebRequest.CookieContainer = New System.Net.CookieContainer
>  myHttpWebRequest.CookieContainer.Add(pCookies)
>
>  Dim sData As String = "a=1"
>  myHttpWebRequest.Method = "POST"
>  myHttpWebRequest.ContentLength = sData.Length
>  myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"
>
>  Dim myStreamWriter As StreamWriter = Nothing
>  myStreamWriter = New StreamWriter(myHttpWebRequest.GetRequestStream())
>  myStreamWriter.Write(psPostData)
>  myStreamWriter.Close()

>  Dim myHttpWebResponse As HttpWebResponse =
>CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
>
>
>It works fine, and I capture the packet again, I find that the required
>cookie is included in the "HTTP POST" packet.
>
>
>In fact, I'm not sure how to use the cookies in this case, I just modify the
>code from books which does not include cookies, and I combine the example for
>using cookies with "POST" method.  So, I'd like to know if I have missed some
>step for a "Get" method with cookies.
>
>BTW, may I know if there has any example on using the Cookies with
>httpwebrequest using "GET" method?
>
>
>Thanks in advance!
>

>
From what I understand of web interaction, it's true, using GET just
appends the item/value pairs to the url.  It does not include a body of
data.
Ex: www.mysite.com?item1=value1&item2=value2
What you could do is put your cookie item/value pairs into the url. 
Otherwise, use POST.

Tom
Author
28 May 2006 2:39 AM
James MA
> "tomb" wrote:
>  From what I understand of web interaction, it's true, using GET just
> appends the item/value pairs to the url.  It does not include a body of
> data.
> Ex: www.mysite.com?item1=value1&item2=value2
> What you could do is put your cookie item/value pairs into the url. 
> Otherwise, use POST.
>
> Tom
>

Thanks for your information. 

However, if I capture the packet from Internet Explorer on the same URL
linke, it will include the cookies value in the "HTTP GET" method.  Since the
server will read the cookie in other way, it cannot be added in the URL.

James
Author
28 May 2006 10:57 AM
Göran Andersson
What tomb said about a GET request not having any body of data is true,
but that is irrelevant as the cookies are sent in the request header,
not in the data body.

For some reason the cookies doesn't seem to end up in the header.
Perhaps you are skipping something that needs to be done to actually
create the header.

I found this code that apparently is working:
http://aspzone.com/blogs/john/archive/2006/05/11/1778.aspx

Try to specifically set the method to "GET", like he does in the code.

James MA wrote:
Show quoteHide quote
> I'm now writing a small program to communicate a web server to simulate a web
> client.  I use te httpwebrequest to talk with the server, and it works find
> for "POST" method, however, when i test other link using "GET" method, i
> found that the cookies data has not included in the request.
>
> Here is the sample:
>
>
>   ' sURL is the URL of server page
>   ' pCookies is a varible contain the cookies data
>   '
>   Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(sURL),
> HttpWebRequest)
>   myHttpWebRequest.UserAgent = _HttpUserAgent
>   myHttpWebRequest.CookieContainer = New System.Net.CookieContainer
>   myHttpWebRequest.CookieContainer.Add(pCookies)
>
>   Dim myHttpWebResponse As HttpWebResponse =
> CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
>
>
> I find that the web server cannot obtain the cookies data in this case, and
> I have capture the packing from my pc, i find that the packet of "HTTP GET"
> does not contain the cookies data.
>
> However, if I change the program to use "POST" method with some dummy data,
>
>   Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(sURL),
> HttpWebRequest)
>   myHttpWebRequest.UserAgent = _HttpUserAgent
>   myHttpWebRequest.CookieContainer = New System.Net.CookieContainer
>   myHttpWebRequest.CookieContainer.Add(pCookies)
>
>   Dim sData As String = "a=1"
>   myHttpWebRequest.Method = "POST"
>   myHttpWebRequest.ContentLength = sData.Length
>   myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"
>
>   Dim myStreamWriter As StreamWriter = Nothing
>   myStreamWriter = New StreamWriter(myHttpWebRequest.GetRequestStream())
>   myStreamWriter.Write(psPostData)
>   myStreamWriter.Close()
>  
>   Dim myHttpWebResponse As HttpWebResponse =
> CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
>
>
> It works fine, and I capture the packet again, I find that the required
> cookie is included in the "HTTP POST" packet.
>
>
> In fact, I'm not sure how to use the cookies in this case, I just modify the
> code from books which does not include cookies, and I combine the example for
> using cookies with "POST" method.  So, I'd like to know if I have missed some
> step for a "Get" method with cookies.
>
> BTW, may I know if there has any example on using the Cookies with
> httpwebrequest using "GET" method?
>
>
> Thanks in advance!
>
Author
29 May 2006 4:01 PM
James MA
Thanks for your information.

However, this example is just to pass an empty cookie container to server,
it may be used when the first time visiting the page.  However, i need the
code for subsequent visit to the web site, so that the CookieContainer shoudl
pass the previous cookie back to the server.  I have already include the
coding in my function, and added the cookies from previous visit (already
checked via debug, the cookies really contain the required data) but it seems
that the httprequest will ignore the CookeContainer for "GET" method.

Thanks a lot!
James

Show quoteHide quote
"Göran Andersson" wrote:

> What tomb said about a GET request not having any body of data is true,
> but that is irrelevant as the cookies are sent in the request header,
> not in the data body.
>
> For some reason the cookies doesn't seem to end up in the header.
> Perhaps you are skipping something that needs to be done to actually
> create the header.
>
> I found this code that apparently is working:
> http://aspzone.com/blogs/john/archive/2006/05/11/1778.aspx
>
> Try to specifically set the method to "GET", like he does in the code.
>
Author
29 May 2006 6:01 PM
Göran Andersson
Have you tried to specify the method, as I suggested?

James MA wrote:
Show quoteHide quote
> Thanks for your information.
>
> However, this example is just to pass an empty cookie container to server,
> it may be used when the first time visiting the page.  However, i need the
> code for subsequent visit to the web site, so that the CookieContainer shoudl
> pass the previous cookie back to the server.  I have already include the
> coding in my function, and added the cookies from previous visit (already
> checked via debug, the cookies really contain the required data) but it seems
> that the httprequest will ignore the CookeContainer for "GET" method.
>
> Thanks a lot!
> James
>
> "Göran Andersson" wrote:
>
>> What tomb said about a GET request not having any body of data is true,
>> but that is irrelevant as the cookies are sent in the request header,
>> not in the data body.
>>
>> For some reason the cookies doesn't seem to end up in the header.
>> Perhaps you are skipping something that needs to be done to actually
>> create the header.
>>
>> I found this code that apparently is working:
>> http://aspzone.com/blogs/john/archive/2006/05/11/1778.aspx
>>
>> Try to specifically set the method to "GET", like he does in the code.
>>
>
Author
30 May 2006 2:17 PM
James MA
Yes, I did.

But the situation getting interesting now, i still studying the problem.
The first time I use GET method with cookies, it works....but after visited
some page within this server with POST mehtod, it come to a page which
require GET method with cookies, the cookies is missing in the request packet.

I have debug many times, the cookies do contain the requried information
before I pass to the httpwebrequest, and it do contain the data before it
make the request.......however, the cokkies is empty when talking with the
server.

Maybe there has some setting missing in my program, still debugging.
I think i need to still the setting of httpwebrequest again before using it.
^_^

Anyway, thanks a lot for your help.
James MA


Show quoteHide quote
"Göran Andersson" wrote:

> Have you tried to specify the method, as I suggested?
>
> James MA wrote:
> > Thanks for your information.
> >
> > However, this example is just to pass an empty cookie container to server,
> > it may be used when the first time visiting the page.  However, i need the
> > code for subsequent visit to the web site, so that the CookieContainer shoudl
> > pass the previous cookie back to the server.  I have already include the
> > coding in my function, and added the cookies from previous visit (already
> > checked via debug, the cookies really contain the required data) but it seems
> > that the httprequest will ignore the CookeContainer for "GET" method.
> >
> > Thanks a lot!
> > James
> >
> > "Göran Andersson" wrote:
> >
> >> What tomb said about a GET request not having any body of data is true,
> >> but that is irrelevant as the cookies are sent in the request header,
> >> not in the data body.
> >>
> >> For some reason the cookies doesn't seem to end up in the header.
> >> Perhaps you are skipping something that needs to be done to actually
> >> create the header.
> >>
> >> I found this code that apparently is working:
> >> http://aspzone.com/blogs/john/archive/2006/05/11/1778.aspx
> >>
> >> Try to specifically set the method to "GET", like he does in the code.
> >>
> >
>