473,398 Members | 2,393 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,398 software developers and data experts.

GET original error message with HttpWebResponse

Hi All,

I am using a WebRequest & WebResponse to GET the contents of an aspx page as
a string (code below).

It's working fine, except that when the aspx page has an error on it, I want
to know precisely what the error was (ie I want to get the error message
that would be displayed if you were viewing the aspx page in a brower).

Instead I always get this error:
The remote server returned an error: (500) Internal Server Error
pointing to the line:
WebResponse myResponse = myRequest.GetResponse();

Can anyone tell me how I can GET the original error text returned to me?

Thanks,

JON
=====
CODE
=====
// Initialize the WebRequest.
WebRequest myRequest =
WebRequest.Create("http://localhost/PageWithError.aspx");

// Return the response.
WebResponse myResponse = myRequest.GetResponse();

// Convert the GET to a string and display it
StreamReader sr = new
StreamReader(myResponse.GetResponseStream(),System .Text.Encoding.Default);
string sBuffer;
sBuffer = sr.ReadToEnd();
sr.Close();
txtDisplayHtml.Text = sBuffer;


Nov 15 '05 #1
2 9492
Jon Maz wrote:
Hi All,

I am using a WebRequest & WebResponse to GET the contents of an aspx
page as a string (code below).

It's working fine, except that when the aspx page has an error on it,
I want to know precisely what the error was (ie I want to get the
error message that would be displayed if you were viewing the aspx
page in a brower).

Instead I always get this error:
The remote server returned an error: (500) Internal Server Error
pointing to the line:
WebResponse myResponse = myRequest.GetResponse();

Can anyone tell me how I can GET the original error text returned to
me?


You mean the actual error page's content?

If a WebException is thrown because of a protocol error, its Response
property will give you access to actual response received from the web
server. Just make sure you check the Status property accordingly:

try {
// Do WebRequest
}
catch (WebException ex) {
if (ex.Status == WebExceptionStatus.ProtocolError) {
HttpWebResponse response = ex.Response as HttpWebResponse;
if (response != null) {
// Process response
}
}
}

Cheers,
--
Joerg Jooss
jo*********@gmx.net

Nov 15 '05 #2
Thanks a lot, Joerg!

Nov 15 '05 #3

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

Similar topics

3
by: James Dunkerley | last post by:
Hi, I am trying to create a class which downloads a web page in the background and then raises an event in the original thread in which it was created. This class is going to be the basis of a...
2
by: sean | last post by:
Hi there, I am trying to call a C# web service from an aspx page, I have the asmx file, a user control file ascx and the aspx file. I have verified that the web service is returning correct...
2
by: Jon Maz | last post by:
Hi All, I am using a WebRequest & WebResponse to GET the contents of an aspx page as a string (code below). It's working fine, except that when the aspx page has an error on it, I want to...
15
by: warlord | last post by:
I have a windows client app that is trying to download a file from a web server but I always get the following error when I call the GetResponse method of the Request object. The remote server...
2
by: Maris Janis Vasilevskis | last post by:
Hi, Is it possible to force HttpWebRequest to do exactly (not approximately) the same as MSXML2.ServerXMLHTTP does? More details. I port JScript to JScript.NET I have a server (ASP invoking...
6
by: Saya | last post by:
Hi, In my app I've done the following: ..... wReq=(HttpWebRequest)WebRequest.Create(someURL); ..... wRes=(HttpWebResponse)(wReq).GetResponse(); On this last line, the GetResponse generates...
4
by: XML newbie: Urgent pls help! | last post by:
I am using VB.Net. My program is to connect to a remote IPAddress. Once, it verifies the login information it should display the SessionID and enable some button . I appreciate your help and thanku...
0
by: Brent | last post by:
After six months flawless operation, I'm suddenly getting this error: "Unable to read data from the transport connection." The code* hasn't changed, and from what I can see, neither have the IP...
0
by: Narendramohan | last post by:
I have written the following code in onstop event of a window service. but i got one error: Unable to Connect to Remote server Error at HttpWebResponse oWebResp =...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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...
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.