473,387 Members | 1,388 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,387 software developers and data experts.

Understanding HttpWebRequest CookieContainer?

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();

-req.CookieContainer gets assigned an empty cookieContainer
req.CookieContainer = cookieContainer;

// Begin Code
CookieContainer cookieContainer = new CookieContainer();
// First hit the login page
HttpWebRequest req =
(HttpWebRequest)HttpWebRequest.Create(loginUri);
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(loginData);
req.ContentLength = loginDataBytes.Length;
Stream stream = req.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
stream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();

// Then grab the content of the desired page
req = (HttpWebRequest)HttpWebRequest.Create(requestUri);
req.CookieContainer = cookieContainer;
req.Method = "GET";
res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
return sr.ReadToEnd();
}
// End Code

I thought that you had to grab the values in the response method and
then fill the container with the cookies and pass it to the second
request.
Rod

Aug 23 '06 #1
5 35230
Rod,

When you set the CookieContainer property on the request, the response
is going to populate that CookieContainer instance with the cookies received
from the response. This is why you can just reassign it to subsequent
requests.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
>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();

-req.CookieContainer gets assigned an empty cookieContainer
req.CookieContainer = cookieContainer;

// Begin Code
CookieContainer cookieContainer = new CookieContainer();
// First hit the login page
HttpWebRequest req =
(HttpWebRequest)HttpWebRequest.Create(loginUri);
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(loginData);
req.ContentLength = loginDataBytes.Length;
Stream stream = req.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
stream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();

// Then grab the content of the desired page
req = (HttpWebRequest)HttpWebRequest.Create(requestUri);
req.CookieContainer = cookieContainer;
req.Method = "GET";
res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
return sr.ReadToEnd();
}
// End Code

I thought that you had to grab the values in the response method and
then fill the container with the cookies and pass it to the second
request.
Rod

Aug 23 '06 #2
I see, if I understand it gets the CookieContainer reference and
assigns the proper cookies, correct? But how does it modify
CookieContainer in order to pass it over filled with cookies? For me it
is confusing because the CookieContainer that is being modified is the
one in the request.CookieContainer not the newly created one.

Rod

Nicholas
Nicholas Paldino [.NET/C# MVP] wrote:
Rod,

When you set the CookieContainer property on the request, the response
is going to populate that CookieContainer instance with the cookies received
from the response. This is why you can just reassign it to subsequent
requests.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
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();

-req.CookieContainer gets assigned an empty cookieContainer
req.CookieContainer = cookieContainer;

// Begin Code
CookieContainer cookieContainer = new CookieContainer();
// First hit the login page
HttpWebRequest req =
(HttpWebRequest)HttpWebRequest.Create(loginUri);
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(loginData);
req.ContentLength = loginDataBytes.Length;
Stream stream = req.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
stream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();

// Then grab the content of the desired page
req = (HttpWebRequest)HttpWebRequest.Create(requestUri);
req.CookieContainer = cookieContainer;
req.Method = "GET";
res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
return sr.ReadToEnd();
}
// End Code

I thought that you had to grab the values in the response method and
then fill the container with the cookies and pass it to the second
request.
Rod
Aug 23 '06 #3
That's the thing, there is no new cookie container. It is assigning the
originally created one (which was assigned to the first request) to the
second request. They share a reference, they don't make copies of the
cookie container.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
>I see, if I understand it gets the CookieContainer reference and
assigns the proper cookies, correct? But how does it modify
CookieContainer in order to pass it over filled with cookies? For me it
is confusing because the CookieContainer that is being modified is the
one in the request.CookieContainer not the newly created one.

Rod

Nicholas
Nicholas Paldino [.NET/C# MVP] wrote:
>Rod,

When you set the CookieContainer property on the request, the
response
is going to populate that CookieContainer instance with the cookies
received
from the response. This is why you can just reassign it to subsequent
requests.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11*********************@p79g2000cwp.googlegr oups.com...
>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();

-req.CookieContainer gets assigned an empty cookieContainer
req.CookieContainer = cookieContainer;

// Begin Code
CookieContainer cookieContainer = new CookieContainer();
// First hit the login page
HttpWebRequest req =
(HttpWebRequest)HttpWebRequest.Create(loginUri);
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(loginData);
req.ContentLength = loginDataBytes.Length;
Stream stream = req.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
stream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();

// Then grab the content of the desired page
req = (HttpWebRequest)HttpWebRequest.Create(requestUri);
req.CookieContainer = cookieContainer;
req.Method = "GET";
res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
return sr.ReadToEnd();
}
// End Code

I thought that you had to grab the values in the response method and
then fill the container with the cookies and pass it to the second
request.
Rod

Aug 23 '06 #4
Nicholas, I am beginning to understand, thank you. One last question:
Why do you need the HTTPresponse.cookies if session cookies goes in the
CookieContainer? Can you give a scenario where you need
wsbresp.cookies?

HttpWebResponse wsbresp = (HttpWebResponse)webreq.GetResponse();
wsbresp.cookies????

Rod

Nicholas Paldino [.NET/C# MVP] wrote:
That's the thing, there is no new cookie container. It is assigning the
originally created one (which was assigned to the first request) to the
second request. They share a reference, they don't make copies of the
cookie container.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
I see, if I understand it gets the CookieContainer reference and
assigns the proper cookies, correct? But how does it modify
CookieContainer in order to pass it over filled with cookies? For me it
is confusing because the CookieContainer that is being modified is the
one in the request.CookieContainer not the newly created one.

Rod

Nicholas
Nicholas Paldino [.NET/C# MVP] wrote:
Rod,

When you set the CookieContainer property on the request, the
response
is going to populate that CookieContainer instance with the cookies
received
from the response. This is why you can just reassign it to subsequent
requests.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
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();

-req.CookieContainer gets assigned an empty cookieContainer
req.CookieContainer = cookieContainer;

// Begin Code
CookieContainer cookieContainer = new CookieContainer();
// First hit the login page
HttpWebRequest req =
(HttpWebRequest)HttpWebRequest.Create(loginUri);
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(loginData);
req.ContentLength = loginDataBytes.Length;
Stream stream = req.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
stream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();

// Then grab the content of the desired page
req = (HttpWebRequest)HttpWebRequest.Create(requestUri);
req.CookieContainer = cookieContainer;
req.Method = "GET";
res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
return sr.ReadToEnd();
}
// End Code

I thought that you had to grab the values in the response method and
then fill the container with the cookies and pass it to the second
request.
Rod
Aug 23 '06 #5
The Cookies property on the HttpWebResponse should return just the
cookies returned by the response, not all the cookies in the container.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Nicholas, I am beginning to understand, thank you. One last question:
Why do you need the HTTPresponse.cookies if session cookies goes in the
CookieContainer? Can you give a scenario where you need
wsbresp.cookies?

HttpWebResponse wsbresp = (HttpWebResponse)webreq.GetResponse();
wsbresp.cookies????

Rod

Nicholas Paldino [.NET/C# MVP] wrote:
>That's the thing, there is no new cookie container. It is assigning the
originally created one (which was assigned to the first request) to the
second request. They share a reference, they don't make copies of the
cookie container.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegr oups.com...
>I see, if I understand it gets the CookieContainer reference and
assigns the proper cookies, correct? But how does it modify
CookieContainer in order to pass it over filled with cookies? For me it
is confusing because the CookieContainer that is being modified is the
one in the request.CookieContainer not the newly created one.

Rod

Nicholas
Nicholas Paldino [.NET/C# MVP] wrote:
Rod,

When you set the CookieContainer property on the request, the
response
is going to populate that CookieContainer instance with the cookies
received
from the response. This is why you can just reassign it to subsequent
requests.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11*********************@p79g2000cwp.googlegr oups.com...
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();

-req.CookieContainer gets assigned an empty cookieContainer
req.CookieContainer = cookieContainer;

// Begin Code
CookieContainer cookieContainer = new CookieContainer();
// First hit the login page
HttpWebRequest req =
(HttpWebRequest)HttpWebRequest.Create(loginUri);
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(loginData);
req.ContentLength = loginDataBytes.Length;
Stream stream = req.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
stream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();

// Then grab the content of the desired page
req = (HttpWebRequest)HttpWebRequest.Create(requestUri);
req.CookieContainer = cookieContainer;
req.Method = "GET";
res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
return sr.ReadToEnd();
}
// End Code

I thought that you had to grab the values in the response method and
then fill the container with the cookies and pass it to the second
request.
Rod


Aug 23 '06 #6

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

Similar topics

0
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...
0
by: Peter Qian | last post by:
Hi, I'm working on a windows form based program that can log into a web service (Apache based, https is used for auth). I was able to post the login data and obtain a sessionID. However I'm not...
2
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....
14
by: tomer | last post by:
Clear DayHello, I have implemented a download manger in .NET that overrides Internet Explorer's bult-in download manager. I using HttpWebRequest/Response to download the files. All is working...
6
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...
0
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...
10
by: rlueneberg | last post by:
I am trying to foward the old sessionID using "Session.SessionID" to an HttpWebRequest CookieContainer so that I can capture the requested page session variables but it is not working as it is...
6
by: nganapat | last post by:
I am trying to post form values to a https web page programmatically using Httpwebrequest but no matter what I do the same login page is returned instead of the next page. I would very much...
1
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?...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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,...
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.