Search code examples
c#restfiddler

Can't get actual error in response for REST request. Fiddler shows me more error details. In .NET all I get is 500 - Internal Server Error


In .NET, I am calling a rest service and getting an exception - 500 Internal Server Error.

HttpWebResponse response = request.GetResponse() as HttpWebResponse

When I analyze this in Fiddler (in TextView), I am getting many details about the proper exception that caused the error.

In my exception object, I can't get this information in the InnerException (it's null) nor in the Response object itself.

Any ideas?


Solution

  • Try looking at the WebException.Response Property:

    catch(WebException ex)
    {
        Console.WriteLine(ex.Message);
    
        if (ex.Status == WebExceptionStatus.ProtocolError)
        {
            Console.WriteLine("Status Code : {0}", ((HttpWebResponse)ex.Response).StatusCode);
            Console.WriteLine("Status Description : {0}", ((HttpWebResponse)ex.Response).StatusDescription);
    
            using (Stream responseStream = ex.Response.GetResponseStream())
            {
                if (responseStream != null)
                {
                    using (StreamReader responseReader = new StreamReader(responseStream))
                    {
                        Console.WriteLine(responseReader.ReadToEnd());
                    }
                }
            }
        }
    }