locked
GET original error message with HttpWebResponse RRS feed

  • Question

  • User-1887956458 posted
    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; PS First posted yesterday to: microsoft.public.dotnet.framework.aspnet, microsoft.public.dotnet.framework, microsoft.public.dotnet.languages.csharp No answers yet...
    Tuesday, February 10, 2004 6:19 AM

All replies

  • User-1391363652 posted
    This is only my guess but... the best you can do is put a try and catch around this code snip: try { WebResponse myResponse = myRequest.GetResponse(); StreamReader sr = new StreamReader(myResponse.GetResponseStream(),System.Text.Encoding.Default); string sBuffer; sBuffer = sr.ReadToEnd(); sr.Close(); txtDisplayHtml.Text = sBuffer; } catch (WebException webexception) { } I think you will receive "Internal Server Error" no matter what. By creating a request you let the target page execute. It finishes completion one way or another and creates some kind of an HTML response which is about as much as you can get. Also, if you have a custom error page set up there you will get back whatever it renders. Maybe that's what you need---a custom error page that creates a more meaningful response. I did some decompiling of the System.Net class and here's what I found: public override WebResponse GetResponse() { ... this.CheckFinalStatus(); ... } private void CheckFinalStatus() { ... array1[0] = NetRes.GetWebStatusCodeString(this.ResponseStatusCode, this._HttpResponse.StatusDescription); exception1 = new WebException(SR.GetString("net_servererror", array1), this._ResponseException, 7, this._HttpResponse); ... The HttpWebRequest class sees the 500 HTTP code, formats an error message, creates an instance of class WebException and throws it. It doesn't look like it has any information about the exception on the other end. Btw, the "net_servererror" resourse string is defined as follows: Resource net_servererror : string Value: "The remote server returned an error: {0}." Looks familiar? :)
    Thursday, February 19, 2004 10:07 PM