473,624 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HttpWebRequest Class - Process Exception Message

Hi,

I am using HttpWebRequest class to communicate with remote server. In some cases the server would return 5xx status code which results in HttpWebRequest object throwing an exception. I, however, still need to access the response stream from the server as it contains information that I need. Any ideas how I can do that?

Thanks,
Konstantin

try
{
HttpWebRequest myRequest = (HttpWebRequest )WebRequest.Cre ate("http://localhost/myUrl");
myRequest.Metho d = "GET";
WebResponse response = myRequest.GetRe sponse();
//If the server returns 5xx status code the next statement executed will be in the catch block
//Nevertheless, I still need the response stream from the server
Stream responseStream = response.GetRes ponseStream();
}
catch (System.Excepti on exc)
{
//Process exception
//How can I access the response stream from here?
}

*************** *************** *************** *************** **********
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Nov 19 '05 #1
1 1623
Hi Konstantin,

By default WebRequest will throw an exception. You have to handle the
WebException and use the resulting ex.Response of it to read from to get the
full content from the server.

// *** Return the Response data
HttpWebResponse WebResponse = null;
try
{
WebResponse = (HttpWebRespons e) loHttp.GetRespo nse();
}
catch(WebExcept ion ex)
{
if (ex.Status == WebExceptionSta tus.ProtocolErr or)
WebResponse = (HttpWebRespons e) ex.Response ;
}

Hope this helps,

+++ Rick ---

--

Rick Strahl
West Wind Technologies
www.west-wind.com
www.west-wind.com/weblog

"Konstantin " <ia********@yah oo.com> wrote in message
news:uE******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

I am using HttpWebRequest class to communicate with remote server. In some
cases the server would return 5xx status code which results in
HttpWebRequest object throwing an exception. I, however, still need to
access the response stream from the server as it contains information that
I need. Any ideas how I can do that?

Thanks,
Konstantin

try
{
HttpWebRequest myRequest =
(HttpWebRequest )WebRequest.Cre ate("http://localhost/myUrl");
myRequest.Metho d = "GET";
WebResponse response = myRequest.GetRe sponse();
//If the server returns 5xx status code the next statement executed will
be in the catch block
//Nevertheless, I still need the response stream from the server
Stream responseStream = response.GetRes ponseStream();
}
catch (System.Excepti on exc)
{
//Process exception
//How can I access the response stream from here?
}

*************** *************** *************** *************** **********
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP &
ASP.NET resources...

Nov 19 '05 #2

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

Similar topics

5
12322
by: Dan Battagin | last post by:
Is there a known bug with the interaction between the HttpWebRequest and the ThreadPool? I current spawn several HttpWebRequest's using BeginGetResponse, and they work for a while, using worker threads from the ThreadPool. However, eventually (relatively quickly) there become fewer and fewer available worker threads in the pool, until there are 0 and a System.InvalidOperationException occurs with the message: "There were not enough free...
10
472
by: Brian Brown | last post by:
I have code which works as an asp.net page that posts an xml file to web page and gets a response back. When the the calls GetResponse() it goes into the page it's posting to to and works fine. When it's been ported to a winform it doesn't work on the GetResponse() call. I think it probably needs credentials but not sure what to use, tried a few ids w/o any luck.
10
19341
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is able to fill that one out just fine. The second form is multipart/form-data. Unfortunately, I haven't been able to fill that out in a way that makes the server happy. I set up a copy of this form at my web site so that I could see exactly what a...
1
12586
by: Jeff B | last post by:
I'm trying to create a simple screen scraping application and I kept getting a System.Net.WebException thrown back with a message of "The operation has timed-out." At first I thought it was some kind of connectivity issue on the machine. It didn't make sense though, because I can open up a browser on the same machine and easily browse the web. I'm stumped. I looked over my code for any errors and just couldn't find what I was doing...
5
5864
by: japslam japslam via DotNetMonster.com | last post by:
Hi all, I have problem when I use HttpWebRequest and take long time to call to my service server. If at that time there are many request comes in semultaneous, I will get this exception System.Net.WebException: The underlying connection was closed: The request was canceled. ---> System.IO.IOException: Unable to read data from the transport connection. ---> System.ObjectDisposedException: Cannot access a disposed object named...
1
2600
by: sfoxover | last post by:
Hi, Could someone please give me some suggestions on how to make this class robust. I need to be able to handle around 20 similtanious requests to this class which causes a web browser to display a waiting message while it requests XML data from a third party server. Some requests can take around 30 seconds. Usually the first time I do the request it always works. If I try again without closing the web browser it will fail sometimes....
6
4177
by: Mike Koerner | last post by:
Hi, I am having problems setting the HttpWebRequest Date header. I understand that it is a restricted header and I do receive the "This header must be modified with the appropriate property." Is there a way to make sure that the date header is sent over or a way to work around this exception? Here's a sample of the code:
1
4096
by: Kevin Landymore | last post by:
I have a vb.net service running under a Domain account. I'm trying to call a web service on our Mainframe (IBM CICS via SSL and Client Certificates) and after a while (1 or 2 days.. thousands of transactions) I'm getting the following errors: First, 2 of these: SoapDriver GotRequestStream System.Net.WebException: The underlying connection was closed: Could not establish secure channel for SSL/TLS. --->...
2
5676
by: =?Utf-8?B?U2ltb25EZXY=?= | last post by:
Hi I have a utility class, called MailHandler, that I wrote to read and operate on emails on an Exchange server using WebDAV. The WebDAV SQL statements are sent using an HttpWebRequest object. This worked fine in a .NET 1.1 project. I have copied the same Mailhandler class into a .NET 2.0 project. This, too, worked fine when I initially tested it, running as a Windows Forms app on my machine (running under my network account). ...
0
8242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8681
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...
1
8341
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
8488
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...
0
7170
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
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
5570
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();...
1
2611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1488
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.