473,721 Members | 3,971 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 8752
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
3923
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
5388
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 the page just sits there as if nothing at all happened. If I remove one line for the textarea,...
6
1876
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
1902
by: Duck Dodgers | last post by:
Here is my situation class base { }; class child1 { int data; }; class child2 {
2
6525
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
1974
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
4586
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
3404
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
2314
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" value="male" /> Male<br /> <input type="radio" name="sex" value="female" /> Female <br /> <input...
0
9373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9227
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9081
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6676
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5992
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3206
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
2
2590
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.