Retrieving cookies from HttpWebResponse despite 404

M

Mr Flibble

Hey!

I get a 404 from a website (this is "correct behaviour") and I'm
interested in the value in the cookie "SMSESSION".

The question is the following code generates an exception due to the
404. How am I still able to read the cookies even though I get an
exception?

Here's the code:

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

if (webResponse.Cookies != null && webResponse.Cookies.Count != 0)
{
foreach (Cookie c in webResponse.Cookies)
{
Console.WriteLine("\t" + c.ToString());
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Mr Fibble,

You can catch the exception, then check the Response property on the
exception. It can be cast to HttpWebResponse, and then you can access the
cookies from there.

Hope this helps.
 
M

Mr Flibble

* Nicholas Paldino said:
Mr Fibble,

You can catch the exception, then check the Response property on the
exception. It can be cast to HttpWebResponse, and then you can access the
cookies from there.

Hope this helps.


Thank you that works a treat!!!
 

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