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

Retrieving cookies from HttpWebResponse despite 404

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());
}
}
May 8 '06 #1
2 3377
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mr Flibble" <mr********@flibbyly.wobbly.n.et> wrote in message
news:e3***********@custnews.inweb.co.uk...
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());
}
}

May 8 '06 #2
* Nicholas Paldino [.NET/C# MVP] wrote:
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!!!
May 10 '06 #3

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

Similar topics

0
by: Pat Allan | last post by:
I've spent half a day on this with no success, so I'm hoping someone can point out what I'm doing wrong. I have an ASP.NET Server-side component that I've created, and as part of its Render...
1
by: Ravi | last post by:
Hi , i am trying to pass the same session Id to all the webrequest, but sometimes the response.cookies returns zero and sometimes one. is this to do something with cookies expire. In this sample...
1
by: SamG | last post by:
Hello, Does anyone know how I can Write a Cookie and then later retrieve it...? Also, Is it possible to write attributes and corresponding values to the cookie file.Needless to say would require...
12
by: Patxi | last post by:
This is the first time I try to use cookies, and despite of reading some tutotials, I'm have real trouble to make it work correctly. My cookie reading code is in Master Page. When I click on a...
1
by: asg | last post by:
I have the code below that will retrieve a cookie that was placed by the server (ie: joes.com) that the document resides on. I want to be able to retrieve a cookie set by another server (ie:...
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...
1
by: sarvesharnie | last post by:
dear folks, i developed a small web browser... on that web browser i need to store the cookies and retrieve the cookies... how to do that . i developed this application C#...
2
by: miraan | last post by:
Hi, I set a php cookie on one page, is it possible to retrieve the same cookie on another webpage on my site or can i just retrieve the cookie from the page that i set the cookie on?
2
by: nishika | last post by:
i have written following code for setting & retrieving cookies:- $lang = 'french'; setcookie('TRADE_LANGUAGE', $lang, time()+3600); print_r($_COOKIE); but while printing it gives only one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.