Can I capture a more meaningful response from a web response in .NET?

Posted on 2012-09-07
Last Modified: 2013-12-16
If I go into fiddler, I can see this nice message,
<?xml version="1.0"?>
    <error_message ident="card_number rejected: the credit card number was entered incorrectly; the check digit does not match

But the .NET try catch just reports this anemic message,
The remote server returned an error: (500) Internal Server Error."&#9;
 System.Net.HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
Question by:wilfordrocks
    LVL 21

    Expert Comment

    Normally to get the body of the response you'd do this:

    string serviceResponse = String.Empty;
    using( WebResponseBase webResponse = webRequest.GetResponse() )
        using( StreamReader reader = new StreamReader( webResponse.GetResponseStream() ) )
            serviceResponse = reader.ReadToEnd();

    Open in new window

    However, I'm a little confused by what you're getting. The 500 error usually would indicate that the server threw some sort of fault/exception, but in this case you seem to be getting a 500 response and a response body. If that's the case you may need to break up the above code so you get the response stream and read it in your catch block.
    LVL 51

    Accepted Solution

    If you want a better way to see the communication during a web transaction use the Charles Web Proxy.

    It will do a better job of isolating the HTTP traffic.

    However, if you are getting 500 errors did you know that Windows actually stores those in this folder?

    Many people don't look there for details about 500 errors.
    LVL 12

    Expert Comment

    Try this

    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

    Author Closing Comment

    Very helpful.  Thank you

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now