wilfordrocks
asked on
Can I capture a more meaningful response from a web response in .NET?
If I go into fiddler, I can see this nice message,
<?xml version="1.0"?>
<response>
<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."	
System.Net.HttpWebResponse webResponse = (HttpWebResponse)webReques t.GetRespo nse();
<?xml version="1.0"?>
<response>
<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."	
System.Net.HttpWebResponse
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.Protoco lError) {
HttpWebResponse response = ex.Response as HttpWebResponse;
if (response != null) {
// Process response
}
}
}
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.Protoco
HttpWebResponse response = ex.Response as HttpWebResponse;
if (response != null) {
// Process response
}
}
}
ASKER
Very helpful. Thank you
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.