473,782 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = (HttpWebRespons e)webRequest.Ge tResponse();

if (webResponse.Co okies != null && webResponse.Coo kies.Count != 0)
{
foreach (Cookie c in webResponse.Coo kies)
{
Console.WriteLi ne("\t" + c.ToString());
}
}
May 8 '06 #1
2 3399
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.co m

"Mr Flibble" <mr********@fli bbyly.wobbly.n. et> wrote in message
news:e3******** ***@custnews.in web.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 = (HttpWebRespons e)webRequest.Ge tResponse();

if (webResponse.Co okies != null && webResponse.Coo kies.Count != 0)
{
foreach (Cookie c in webResponse.Coo kies)
{
Console.WriteLi ne("\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
1456
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 function, it requests another webpage (which the user defines) through an HttpWebRequest object. This page that is being called requires the cookie objects that are sent to the page hosting the control. So, as shown in the code below, I'm looping...
1
4206
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 code the line Console.WriteLine("cookies1 count : " + firstResponse.Count.ToString()); and Console.WriteLine("second Cookie : " + secondResponse.Cookies.Count.ToString());
1
3630
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 this in .Net (VB.Net or C#)... A sample code or an article on how to accomplish this would be very much appreciated... Thanks,
12
2672
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 test button (in a WEb User Control inside the aspx page), cookie value is displayed in Master Page but if I go to another page, no value is displayed... I need to be able tu use cookie's value across all the site... What am I doing wrong ? Code...
1
1227
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: freds.com) but I want to retrieve it from a page served by joes.com. This code apparently using 'document.cookie' determines the server that the document resides on and looks for cookies from that server. Anybody know of code that will let you...
6
8755
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 other link using "GET" method, i found that the cookies data has not included in the request. Here is the sample: ' sURL is the URL of server page ' pCookies is a varible contain the cookies data
1
1402
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# windows application... please help me out. regards sarvesh
2
1619
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
1834
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 that is "Array ( => 3p4l3oq93b5tet0r7lea77tca0 ) " i have checked in my browser 2 cookies are been created PHPSESSID & TRADE_LANGUAGE but while printing only one gets printed,
0
9479
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10311
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.