2

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...

Community
  • 1
  • 1
GrantDG
  • 401
  • 1
  • 7
  • 14
  • [Read the manual](http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx): _"**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](http://stackoverflow.com/questions/11828843/c-sharp-webexception-how-to-get-whole-response-with-a-body). – CodeCaster Oct 19 '12 at 07:38
  • 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 Oct 19 '12 at 07:02
  • @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 correct – GrantDG Oct 21 '12 at 23:25
  • @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 framework – GrantDG Oct 21 '12 at 23:30
  • 1
    @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 Oct 22 '12 at 07:12
  • FWIW, this looks like a dupe of http://stackoverflow.com/questions/692342/net-httpwebrequest-getresponse-raises-exception-when-http-status-code-400-ba/692369#692369 as well. – EricLaw Oct 25 '12 at 22:22
  • I've seen this, too. The `GetResponseStream()` method on a `WebException`'s `Response` property throws `ArgumentException` with the content "Stream was not readable." –  Feb 12 '13 at 20:41

1 Answers1

2

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.

Community
  • 1
  • 1
mobese46
  • 241
  • 1
  • 7