473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 8729
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Dave Opstad | last post by:
In this snippet: d = {'x': 1} value = d.get('x', bigscaryfunction()) the bigscaryfunction is always called, even though 'x' is a valid key. Is there a "short-circuit" version of get that...
1
by: Pete Mahoney | last post by:
Ok I use a textarea to store data input by the user, and then upon them clicking the submit button I store this data to a database. The problem is once the user inputs too much data (about 3...
6
by: Eitan | last post by:
Hello, I would like to know about the methods : post and get of ASP. What is the difference between them. Need some samples, and explanation, please. Thanks :)
5
by: Duck Dodgers | last post by:
Here is my situation class base { }; class child1 { int data; }; class child2 {
2
by: Tom S | last post by:
I'm using ASP.NET with C# as my code behind and I figure out how to get the above process to work. I've found many examples online for using the 'method' member to "POST", but none for setting it...
2
by: martyn_wynne | last post by:
Hi, I have found a odd one, my submit button is not submitting on a method="get" form after using any form of DataBind? Has anyone struck this problem before? here is snipits of the code as...
5
by: Jeff | last post by:
Visual Studio 2003 DotNet framework 1.1 Windows 2000 Pro I create two pages in an Asp.net application, one is html page with a form in it: .... <form id="testForm" method="post"...
1
by: Jim Carlock | last post by:
Let's not argue about semantics. Let's talk about semasiology. Do specifications exist for using mixed case, upper case, lower case for the contents of the method employed? My own preferences...
7
by: Spencer Killen | last post by:
Hey I have a short little poll thing and i'd like to transfer the results to another page through url like ...com/?x=12&y=55 hers what I got so far: by the way my website doesn't let me use the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.