2
votes

This is similar to a question asked here: httpWebRequest - get error content but I'm hoping to provide some more information in hope of an answer.

I am integrating with the LinkedIn API using C#. When I make an invalid call to the API, it can return a HTTP response like such (from Fiddler):

HTTP/1.1 404 Not Found
Server: Apache-Coyote/1.1
x-li-request-id: VL2EER2ROJ
Date: Tue, 16 Oct 2012 08:52:59 GMT
Vary: *
x-li-format: xml
Content-Type: text/xml;charset=UTF-8
Content-Length: 266

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<error>
  <status>404</status>
  <timestamp>1350377580000</timestamp>
  <request-id>VL2EER2ROJ</request-id>
  <error-code>0</error-code>
  <message>Could not find member {/people/XXXXXX}</message>
</error>

I would specifically like to get at the error xml, however HttpWebRequest seems to simply throw the exception and then has nothing to do with the content. In my debugger, I can see that the content length is 266, but any attempt to call (System.Net.WebException).Response.GetResponseStream() results in a stream was not readable exception.

This method of returning error messages within a non 200 HTTP status response seems quite popular in web APIs - I'm hoping there is a way to get at these responses...

1
What version of .NEt is this? This sounds like a bug. Get a system.net trace log and use that to create a bug report on msdn community site. BTW can you show your code? Maybe there is something there that can be changed to make this work?feroze
Read the manual: "Note: If a WebException is thrown, use the Response and Status properties of the exception to determine the response from the server." See also this duplicate.CodeCaster
@CodeCaster : As stated in the question an attempt to access the Response stream results in a "Cannot read from stream" exception. The answer supplied in the referenced duplicate is not correctGrantDG
@feroze This is happening in .NET 4.0 (and I assume for all earlier versions). I tend to agree with you that this is a bug in the frameworkGrantDG
@GrantDG please show your code. If you use catch (WebException wex) { var resp = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd(); } it should work. This is most probably not a bug in the framework but one in your code, otherwise lots of people would've encountered it.CodeCaster

1 Answers

2
votes

You'll probably need to hit the socket connection directly to get the response xml.

See this answer: https://stackoverflow.com/a/6755142

I'm running into the same thing while trying to implement Amazon's Off Amazon Payments API. If I pass an invalid transaction id, they return a 404 error...not what I was expecting.

Also of note: I'm using async when sending the http post and this 404 throws a WebException on the BeginGetResponse method; not while trying to read the response in the callback. Conversely, 400 Bad Request WebException's, are raised while reading the response stream in the callback.