469,575 Members | 1,377 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

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

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!

May 26 '06 #1
6 8425
James MA wrote:
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
May 27 '06 #2
> "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
May 28 '06 #3
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/archiv...5/11/1778.aspx

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

James MA wrote:
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!

May 28 '06 #4
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/archiv...5/11/1778.aspx

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


May 29 '06 #5
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/archiv...5/11/1778.aspx

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

May 29 '06 #6
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
"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/archiv...5/11/1778.aspx

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

May 30 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Dave Opstad | last post: by
5 posts views Thread by Duck Dodgers | last post: by
2 posts views Thread by Tom S | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.