httpwebrequence cookie problem

O

octavio.filipe

Hi,

I'm trying to use the following code:


HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
webRequest.CookieContainer = new CookieContainer();

HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse();


This code let me get the cookie (cookie PHPSESSID) I need, from
webResponse.
I will need this cookie to get the page that i really need.
Now I will copy the cookie to the HttpWebRequest that i will use to get the
real needed page:


HttpWebRequest httpWebRequestLogin =
HttpWebRequest.Create(LOGIN_URL_LOGIN) as HttpWebRequest;
httpWebRequestLogin.CookieContainer = new CookieContainer();
//note: webResponse.Cookies[0] have the PHPSESSID cookie (i know this)
httpWebRequestLogin.CookieContainer.Add(webResponse.Cookies[0]);
httpWebRequestLogin.Method="POST";
httpWebRequestLogin.ContentType="application/x-www-form-urlencoded";

StreamWriter requestWriter = new StreamWriter
(httpWebRequestLogin.GetRequestStream());
requestWriter.Write(data);
requestWriter.Close();

//lets try to get the page
HttpWebResponse httpWebResponseLogin =
(HttpWebResponse) httpWebRequestLogin.GetResponse();


The page didn't come out! Because the cookie PHPSESSID wasn't on the
httpWebRequestLogin, I think!
So what in the name of LORD am I doing wrong (in the process of copying the
cookie from the webResponse to the httWebRequestLogin)?


Please help me...
Thanks on advance,
o.f
 
F

Feroze [msft]

Why are you creating a new cookiecontainer for the next request? You should
just reuse the first cookiecontainer that you created, and it should just
work fine. WebRequest will send the cookie in that container to the new
server, after making sure that the domains etc match.
 
O

o.f

What you are saying is that I should do something like this:

CookieContainer cc = webRequest.CookieContainer;
httpWebRequestLogin.CookieContainer = cc;

or should I use this:

CookieContainer cc = new CookieContainer();
cc.add(webResponse.Cookies[0]);

My doubt is: should i copy the CookieContainer from the WebRequest, or
should I create a CookieContainer and add the Cookie that i whant from
webResponse?
This doubt exists because if I debug the response I can see the cookie
i whant to copy, but if i debug the request i can't see the cookie i
want anywhere!

Thanks
o.f
 
F

Feroze [msft]

You shoud use the first option:

CookieContainer cc = new CookieContainer();
httpWebRequest.CookieContainer = cc;

and make sure you use the same cookiecontainer for all webrequest's

--
feroze

-----------------
This posting is provided as-is. It offers no warranties and assigns no
rights.

See http://weblogs.asp.net/feroze_daud for System.Net related posts.
----------------

o.f said:
What you are saying is that I should do something like this:

CookieContainer cc = webRequest.CookieContainer;
httpWebRequestLogin.CookieContainer = cc;

or should I use this:

CookieContainer cc = new CookieContainer();
cc.add(webResponse.Cookies[0]);

My doubt is: should i copy the CookieContainer from the WebRequest, or
should I create a CookieContainer and add the Cookie that i whant from
webResponse?
This doubt exists because if I debug the response I can see the cookie
i whant to copy, but if i debug the request i can't see the cookie i
want anywhere!

Thanks
o.f

Why are you creating a new cookiecontainer for the next request? You
should
just reuse the first cookiecontainer that you created, and it should just
work fine. WebRequest will send the cookie in that container to the new
server, after making sure that the domains etc match.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top