469,573 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,573 developers. It's quick & easy.

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 34869
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by TJO | last post: by
reply views Thread by rlueneberg | last post: by
1 post views Thread by ALA | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.