473,848 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HttpWebRequest & HttpWebResponse Cookies

I have a windows form application that generates a request, downloads an image, and waits the user
to enter in login info. Unfortunately, this image is dynamic and based on session data. I have
read documents on the CookieCollectio n property of HttpWebRequest. Currently, I have the
functionality in my code to be able to accept cookies, and return them upon a new HttpWebRequest;
however, upon further inspection of the returning HttpWebResponse .Cookies property, I notice that
the cookies that are supposed to be there regarding my session data are not returned (one such
cookie named, JSESSIONID). What other methods are there available to me to retrieve cookies that
would contain session data?

Here is a code snippet (please excuse the wordwrap)

Dim strURL As String = "https://www.thisurlisat est.com/"
Dim objHTTPRequest As HttpWebRequest = CType(WebReques t.Create(strURL ), HttpWebRequest)
objHTTPRequest. CookieContainer = New CookieContainer ()
Dim objHTTPResponse As HttpWebResponse = CType(objHTTPRe quest.GetRespon se(), HttpWebResponse )
Dim objReceiveStrea m As Stream = objHTTPResponse .GetResponseStr eam()
' I successfully retrieve the cookies here
m_Cookies = objHTTPResponse .Cookies
Dim objCookie As Cookie
For Each objCookie In m_Cookies
Console.WriteLi ne(objCookie.Na me + "=" + objCookie.Value )
Next

' This only outputs this: language_pref=e n_US
' There should be more cookies that contain session id data

Thanks in advance for any light you can shed on this matter,

Jeff Cheung

Nov 20 '05 #1
16 11068
Cor
Hi Cheung,

Do you have multiple webservers that you use the cookieCollectio n?

Otherwise you can have a look at the normal HttpRequest.coo kie class

It is much easier to handle (if you dont forget to set the live time)

I hope this helps

Cor
Nov 20 '05 #2
Cor,

I'm not passing the cookie collection to multiple webservers; however, this is a windows form
application. How do you propose that I get the System.Web.Http Request object in my form application?

I did find the solution to my problem; however, it brought upon a bigger issue for me unfortunately.
I ended up looking at the Set-Cookie item in the Headers property in the HttpWebResponse object,
and adding that value to the Cookie item in the Headers property in the HttpWebRequest object. This
works flawlessly as long as I do not pass a CookieCollectio n in the HttpWebRequest.

According to this link:

http://authors.aspalliance.com/damia...x.vb.view.aspx

if I populate the CookieCollectio n, this will override the Cookie header in the request object.

So my problem now is that I'm able to successfully login and retain my session, but any subsequent
page request returns nothing in the ResponseStream from HttpWebResponse .GetResponseStr eam().
Looking at the HttpWebResponse .StatusCode yields 200, but the content-length is always 0 whenever I
make any requests. Any ideas?

Thanks in advance,

Jeff Cheung

Cor wrote:
Hi Cheung,

Do you have multiple webservers that you use the cookieCollectio n?

Otherwise you can have a look at the normal HttpRequest.coo kie class

It is much easier to handle (if you dont forget to set the live time)

I hope this helps

Cor

Nov 20 '05 #3
Cor
Hi Cheung,

I never used that cookiecontainer because the other ones are in my opinion
more simple.

But my problem with cookies is always that I forget to set the livetime of a
cookie, it disapears immidiatly.

I did not see that in your code, maybe it is your problem also, so before I
start checking things that I never did before, can you check that yourself?

Cor
Nov 20 '05 #4
Cor,

Thanks again for your response. According to the RFC, the Set-Cookie header in the response is
responsible for setting the "live time" of the cookie. I say this with the assumption that you are
talking about the expires attribute for the cookie. The server should be responsible for setting
the expiry date of the cookie; however, I did set it to expire a month explicitly when I read your
message just to test it out. Unfortunately, I still get 0 bytes returned... But I know my session
is still intact because the response doesn't return any new Set-Cookie headers. If my session had
reset, then there would be a new Set-Cookie header containing the Session ID.

Regards,

Jeff Cheung

Cor wrote:
Hi Cheung,

I never used that cookiecontainer because the other ones are in my opinion
more simple.

But my problem with cookies is always that I forget to set the livetime of a
cookie, it disapears immidiatly.

I did not see that in your code, maybe it is your problem also, so before I
start checking things that I never did before, can you check that yourself?

Cor

Nov 20 '05 #5
Cor
Hi Jeff,

I become confused. I can not see how a windowform application can set a
cookie.

The cookie is for the connection from the webform and the user to check that
the right user answers (one of the reasons for a cookie).

Can you explain something more, because I think I am on the wrong track.

Cor
Nov 20 '05 #6
Cor
Hi Jeff,

I become confused. I can not see how a windowform application can set a
cookie.

The cookie is for the connection from the webform and the user to check that
the right user answers (one of the reasons for a cookie).

Can you explain something more, because I think I am on the wrong track.

Cor
Nov 20 '05 #7
Cor,

Sure thing... My windows form application has HttpWebRequest and HttpWebResponse objects. You are
correct, since it's not a webform, I have no need to use the Response.Cookie s method. The
differences are:

ASP.NET application
- You are writing the cookies to the client's browser

My application
- I am writing the cookies to the HttpWebRequest object, since the HttpWebRequest is going to be
making the calls to the webserver.

This goes back to my original question. The documentation in MSDN directs me to use the
CookieCollectio n in order to maintain any cookies in my subsequent requests since the HttpWebRequest
is designed to only make one request to a URI. In essence, my application is more of a web browser
with the exception that I do not have to render any of the html that I parse. My goal is to
impersonate a user logging in to a predefined site, and automating administrative tasks. Let me
know if this clears things up.

Regards,

Jeff Cheung

Cor wrote:
Hi Jeff,

I become confused. I can not see how a windowform application can set a
cookie.

The cookie is for the connection from the webform and the user to check that
the right user answers (one of the reasons for a cookie).

Can you explain something more, because I think I am on the wrong track.

Cor

Nov 20 '05 #8
Cor,

Sure thing... My windows form application has HttpWebRequest and HttpWebResponse objects. You are
correct, since it's not a webform, I have no need to use the Response.Cookie s method. The
differences are:

ASP.NET application
- You are writing the cookies to the client's browser

My application
- I am writing the cookies to the HttpWebRequest object, since the HttpWebRequest is going to be
making the calls to the webserver.

This goes back to my original question. The documentation in MSDN directs me to use the
CookieCollectio n in order to maintain any cookies in my subsequent requests since the HttpWebRequest
is designed to only make one request to a URI. In essence, my application is more of a web browser
with the exception that I do not have to render any of the html that I parse. My goal is to
impersonate a user logging in to a predefined site, and automating administrative tasks. Let me
know if this clears things up.

Regards,

Jeff Cheung

Cor wrote:
Hi Jeff,

I become confused. I can not see how a windowform application can set a
cookie.

The cookie is for the connection from the webform and the user to check that
the right user answers (one of the reasons for a cookie).

Can you explain something more, because I think I am on the wrong track.

Cor

Nov 20 '05 #9
Cor
Hi Jeff,

It soundsvery difficult, but when your program it is a kind of webbrowser,
it needs the cookies from the client.

As far as I know handles the classes you use the cookies from a client from
the side of the webserver.

But you can of course try to read the cookies from the client direct, I did
try that also but stopped, because I could not find an enumerating for the
cookie container and because that it can be on every computer (and specialy
every Microsoft OS) on another place I stopped with it.

But as far as I remember me the cookie is not that difficult file to read, I
thought just a kind of textfile.

Cor
Sure thing... My windows form application has HttpWebRequest and HttpWebResponse objects. You are correct, since it's not a webform, I have no need to use the Response.Cookie s method. The differences are:

ASP.NET application
- You are writing the cookies to the client's browser

My application
- I am writing the cookies to the HttpWebRequest object, since the HttpWebRequest is going to be making the calls to the webserver.

This goes back to my original question. The documentation in MSDN directs me to use the CookieCollectio n in order to maintain any cookies in my subsequent requests since the HttpWebRequest is designed to only make one request to a URI. In essence, my application is more of a web browser with the exception that I do not have to render any of the html that I parse. My goal is to impersonate a user logging in to a predefined site, and automating administrative tasks. Let me know if this clears things up.

Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3429
by: TJO | last post by:
Can someone at MS please reply to this. I am trying to post data so a web form via ssl with the following code. I keep getting this error: "The underlying connection was closed: Could not establish secure channel for SSL/TLS" private void mainHttpCalls(string postData) { HttpWebRequest objRequest1 ; HttpWebRequest objRequest2 ;
6
3096
by: omyek | last post by:
I'm trying to mimic the browsing of a webpage using an HttpWebRequest. I've had a lot of luck with it so far, including logging into pages, posting form data, and even collecting and using cookies. However, I ran into a scenario that I'm baffled by. I have a website which requires a user to login. This is nothing new and I was able to successfully log in. For our case, let's say the URL to the login page is http://login.html
0
6090
by: Sivashankaran Vaidhyalingam | last post by:
Hi folks, I have an aspx application App A hosted in a server which is inside the intranet . I need to serve pages from this application _through_ another application App B which acts as a proxy . App B just has a HttpHandler which recieves the request for pages (actualy residing in App A), creates a HttpWebRequest object (which inturn reads the context.HttpRequest object) and serves the page with HttpWebResponse object (writing the...
2
6814
by: TK | last post by:
I have a trouble to get web resopnse from an aspx page which is secured by Forms Authentication with custom user account database. My client application is a console application but not a browser. I want to download a file from my webapplication. I've learned that the NetworkCredential class gives a way to go but no luck. My code is as following...just dump out the web response for debugging. // C# public void Download(string username,...
0
1516
by: Alex Papadimoulis | last post by:
Hey Group, I'm in the process of converting an ASP-based site to an ASP.NET site and built a control that wraps around an ASP page. The control simply does a GET to the same server to render the ASP content, and then just writes it to the page. In development and testing, there were no problems. Once deployed to production, after the site runs for a few hours, it starts to slow down and throw countless "Operation Has Timed Out"...
0
2042
by: Alex Papadimoulis | last post by:
Hey Group, I'm in the process of converting an ASP-based site to an ASP.NET site and built a control that wraps around an ASP page. The control simply does a GET to the same server to render the ASP content, and then just writes it to the page. In development and testing, there were no problems. Once deployed to production, after the site runs for a few hours, it starts to slow down and throw countless "Operation Has Timed Out"...
6
8762
by: James MA | last post by:
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
5
35296
by: rlueneberg | last post by:
I am totally confused. Can someone please illuminate what is going on under the hood in this piece of code from John Lewis. My main confusion is how the cookieContainer can be passed to the subsequent request if it is not assigned anywhere? So far this is what I understand: -Creates New cookieContainer CookieContainer cookieContainer = new CookieContainer();
1
5690
by: ALA | last post by:
Hi, does anybody know if it is possible to pass the SessionID with a web request by using a cookie so that the invoked page in the same domain can access the session objects of the current user? HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.CookieContainer = new CookieContainer(); HttpCookie sessionCookie = HttpContext.Current.Request.Cookies;
0
9893
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11001
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
10348
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...
0
9500
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7894
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
5735
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
4544
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
4137
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3175
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.