failed to get cookie

G

Guest

I'm trying to login to a website with HttpWebRequest. The login page returns
a cookie and a HTTP 302 to redirect to the main page. I only need the cookie
to request the page I really need.

What I did is that:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.AllowAutoRedirect = false;

req.CookieContainer = new CookieContainer();
HttpWebResponse resp= (HttpWebResponse)req.GetResponse();

Now I expected the cookie in resp.Cookies, but this is empty
(resp.Cookies.Count == 0).

I sniffed the network traffic so I know exactly what came back from the
server:

HTTP/1.1 302 Moved
Date: Thu, 11 Nov 2004 12:24:32 GMT
Server: Apache/1.3.31 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2
mod_bwlimited/1.4 PHP/4.3.8 FrontPage/5.0.2.2634a mod_ssl/2.8.19
OpenSSL/0.9.6b
Set-Cookie: SessionId=2622-113773869; path=/; expires=Mon Jan 10 12:24:32
2005 GMT
Location: /cgi-bin/epg/myroom.cgi
Transfer-Encoding: chunked
Content-Type: text/plain

0

The set-cookie header is there! So why is resp.Cookies empty?

When I try with req.AllowAutoRedirect=true the redirect takes me to the main
page BUT also without sending the cookie, so the main page redirects back to
the login page.

Cheers,
Michael
 
G

Guest

Am I the first one to see this problem?

I can work around it when I parse the resp.Headers collection instead of
resp.Cookies. The Set-Cookie header is in resp.Headers, but I would like to
know, how/if this works with resp.Cookies.

Michael
 
G

Guest

Nope!
I loop through resp.Headers, search for the Set-Cookie header and parse it.

Michael
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top