473,554 Members | 2,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 myHttpWebReques t As HttpWebRequest = CType(WebReques t.Create(sURL),
HttpWebRequest)
myHttpWebReques t.UserAgent = _HttpUserAgent
myHttpWebReques t.CookieContain er = New System.Net.Cook ieContainer
myHttpWebReques t.CookieContain er.Add(pCookies )

Dim myHttpWebRespon se As HttpWebResponse =
CType(myHttpWeb Request.GetResp onse(), 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 myHttpWebReques t As HttpWebRequest = CType(WebReques t.Create(sURL),
HttpWebRequest)
myHttpWebReques t.UserAgent = _HttpUserAgent
myHttpWebReques t.CookieContain er = New System.Net.Cook ieContainer
myHttpWebReques t.CookieContain er.Add(pCookies )

Dim sData As String = "a=1"
myHttpWebReques t.Method = "POST"
myHttpWebReques t.ContentLength = sData.Length
myHttpWebReques t.ContentType = "applicatio n/x-www-form-urlencoded"

Dim myStreamWriter As StreamWriter = Nothing
myStreamWriter = New StreamWriter(my HttpWebRequest. GetRequestStrea m())
myStreamWriter. Write(psPostDat a)
myStreamWriter. Close()

Dim myHttpWebRespon se As HttpWebResponse =
CType(myHttpWeb Request.GetResp onse(), 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 8739
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 myHttpWebReques t As HttpWebRequest = CType(WebReques t.Create(sURL),
HttpWebRequest )
myHttpWebReques t.UserAgent = _HttpUserAgent
myHttpWebReques t.CookieContain er = New System.Net.Cook ieContainer
myHttpWebReques t.CookieContain er.Add(pCookies )

Dim myHttpWebRespon se As HttpWebResponse =
CType(myHttpWe bRequest.GetRes ponse(), 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 myHttpWebReques t As HttpWebRequest = CType(WebReques t.Create(sURL),
HttpWebRequest )
myHttpWebReques t.UserAgent = _HttpUserAgent
myHttpWebReques t.CookieContain er = New System.Net.Cook ieContainer
myHttpWebReques t.CookieContain er.Add(pCookies )

Dim sData As String = "a=1"
myHttpWebReques t.Method = "POST"
myHttpWebReques t.ContentLength = sData.Length
myHttpWebReques t.ContentType = "applicatio n/x-www-form-urlencoded"

Dim myStreamWriter As StreamWriter = Nothing
myStreamWriter = New StreamWriter(my HttpWebRequest. GetRequestStrea m())
myStreamWriter. Write(psPostDat a)
myStreamWriter. Close()

Dim myHttpWebRespon se As HttpWebResponse =
CType(myHttpWe bRequest.GetRes ponse(), 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
httpwebreque st 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 myHttpWebReques t As HttpWebRequest = CType(WebReques t.Create(sURL),
HttpWebRequest)
myHttpWebReques t.UserAgent = _HttpUserAgent
myHttpWebReques t.CookieContain er = New System.Net.Cook ieContainer
myHttpWebReques t.CookieContain er.Add(pCookies )

Dim myHttpWebRespon se As HttpWebResponse =
CType(myHttpWeb Request.GetResp onse(), 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 myHttpWebReques t As HttpWebRequest = CType(WebReques t.Create(sURL),
HttpWebRequest)
myHttpWebReques t.UserAgent = _HttpUserAgent
myHttpWebReques t.CookieContain er = New System.Net.Cook ieContainer
myHttpWebReques t.CookieContain er.Add(pCookies )

Dim sData As String = "a=1"
myHttpWebReques t.Method = "POST"
myHttpWebReques t.ContentLength = sData.Length
myHttpWebReques t.ContentType = "applicatio n/x-www-form-urlencoded"

Dim myStreamWriter As StreamWriter = Nothing
myStreamWriter = New StreamWriter(my HttpWebRequest. GetRequestStrea m())
myStreamWriter. Write(psPostDat a)
myStreamWriter. Close()

Dim myHttpWebRespon se As HttpWebResponse =
CType(myHttpWeb Request.GetResp onse(), 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.......h owever, 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
3908
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 doesn't evaluate the second argument if the first is a valid key? For now I'll code around it, but this behavior surprised me a bit...
1
5356
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 paragraphs or 2020 characters) when they click on the submit button nothing happens. When I say nothing happens I mean just that, nothing at all happens...
6
1870
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
1891
by: Duck Dodgers | last post by:
Here is my situation class base { }; class child1 { int data; }; class child2 {
2
6519
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 to and using "GET". Any help would be greatly appreciated. Thanks! Tom S
2
1972
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 an example <form id="frmUserSearch" method="get" runat="server">
5
4563
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" action="test.aspx" runat="server"> <input type="hidden" id="hiddenTest" value="hello, world"> <input type="submit" id="btnSubmit" value="submit">
1
3393
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 lean towards the all lower-case model, simply because it reads a little easier when deployed with other XHTML semaphorings. --
7
2306
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 post method <form name="input" action="index2.htm" method="get"> Name: <input type="text" name="user" /> <br /> <input type="radio" name="sex"...
0
7584
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7512
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8023
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7879
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6129
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5144
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1117
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
828
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.