Library tutorials & articles
Retrieving HTTP content in .NET
HTTP Cookies
HTTP Cookies are a state management implementation of the HTTP protocol and many Web pages require them. If you're using remote HTTP functionality to drive a Web site (following URLs and the like) you will in many case have to be able to support cookies.
Cookies work by storing tokens on the client side, so the client side is really responsible for managing any cookie created. Normally a browser manages all of this for you, but here there's no browser to help out in an application front end and we're responsible for tracking this state ourselves. This means when the server assigns a cookie for one request, the client must hang on to it and send it back to the server on the next request where it applies (based on the Web site and virtual directory). HttpWebRequest and HttpWebResponse provide the container to hold cookies both for the sending and receiving ends but it doesn't automatically persist them so that becomes your responsibility.
Because the Cookie collections are nicely abstracted in these objects it's fairly easy to save and restore them. The key to make this work is to have a persistent object reference to the cookie collection and then reuse the same cookie store each time.
To do this let's assume you are running the request on a form (or some other class – this in the example below). You'd create a property called Cookies:
CookieCollection Cookies;
On the Request end of the connection before the request is sent to the server you can then check whether there's a previously saved set of cookies and if so use them:
Request.CookieContainer = new CookieContainer();
if (this.Cookies != null &&
this.Cookies.Count > 0)
Request.CookieContainer.Add(this.Cookies);
So, if you previously had retrieved cookies, they were stored in the Cookies property and then added back into the Request's CookieContainer property. CookieContainer is a collection of cookie collections – it's meant to be able to store cookies for multiple sites. Here I only deal with tracking a single set of cookies for a single set of requests.
On the receiving end once the request headers have been retrieved after the call to GetWebResponse(), you then use code like the following:
// *** Save the cookies on the persistent object
if (Response.Cookies.Count > 0)
this.Cookies = Response.Cookies;
This saves the cookies collection until the next request when it is then reassigned to the Request which sends it to the server. Note, that this is a very simplistic cookie management approach that will work only if a single or a single set of cookies is set on a given Web site. If multiple cookies are set in multiple different places of the site you will actually have to retrieve the individual cookies and individually store them into the Cookie collection. Here's some code that demonstrates:
if (loWebResponse.Cookies.Count > 0)
if (this.Cookies == null)
{
this.Cookies = loWebResponse.Cookies;
}
else
{
// If we already have cookies update list
foreach (Cookie oRespCookie in
loWebResponse.Cookies)
{
bool bMatch = false;
foreach(Cookie oReqCookie in
this.oCookies) {
if (oReqCookie.Name ==
oRespCookie.Name) {
oReqCookie.Value =
oRespCookie.Name;
bMatch = true;
break;
}
}
if (!bMatch)
this.Cookies.Add(oRespCookie);
}
}
}
This should give you a good starting point. This code still doesn't deal with things like domains and virtual paths and also doesn't deal with saved cookies, but for most applications the above should be sufficient.
Related articles
Related discussion
-
VS.NET/sql server installation problem
by daspeac (4 replies)
-
Unable to access AxInterop.AcoPdflib.dll on 64 bit OS
by Shaila14041981 (0 replies)
-
connect to .dbf files
by daspeac (5 replies)
-
Binary Studio | software development outsourcing Ukraine
by shane124 (4 replies)
-
Research topic in software
by reachsangeethamathew (0 replies)
Related podcasts
-
More jQuery in ASP.NET
In this episode Chris Brandsma, Rick Strahl, Dave Ward, Bertrand Le Roy, and Scott Koon conclude their discussion of Microsoft's jQuery in ASP.NET announcement1.This episode of the Alt.NET Podcast is brought to you by LLBLGen Pro, the most mature O/R mapper and code generator out there.Are ...
Events coming up
-
Dec
9
GL.net Group Meeting - December 2009
Gloucester, United Kingdom
The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.
if (oReqCookie.Name ==
oRespCookie.Name) {
oReqCookie.Value =
oRespCookie.Name;
Do you mean?
if (oReqCookie.Name ==oRespCookie.Name) {
oReqCookie.Value =
oRespCookie.Value;
You have a paragraph that states:
"The most common use of delegates is an eventhandler, which uses the delegate to fire events. When the event publisher fires the event method, the delegate that is assigned to handle the event is called and you're event subscriber object then can simply handle the event by implementing a method in your class."
Unfortunately, for someone trying desparately to learn C#, this sounds like:
"blah blah blah blah blah blah"
I'm sorry but could you publish a REAL SIMPLE example and explanation of asynch calls in Asp.net/C#. I have been all over the web looking for one and they all assume you already know C# so all the delegate calling callback calling method calling delegate calling calling callingcallingcalling.................has just finally gotten on my nerves.
hi
i have similar problem like you (in a previous time) with in using httwebrequest to login in a website... especially how to handle event clicking ex: onkeypress="checklogin()"
i hope you can help me to solve the problem..
thnx b4
hi
I use httpwebrequest to login into a website (using Networkcrendential). When i got that page i need to click a link. How can we achieve this (axwebbrowser ?) and it contains frames. And also after clicking that menu in second page i have to click a Javascript button. Is there anyway to achieve this.
Rajesh
Also, you should consider having your class implement the IDisposable interface. And in that implementation clean up, close, and release the resources used by your private variables.
This thread is for discussions of Retrieving HTTP content in .NET.