I am working on a client for a REST API. The server returns 4xx responses as a matter of course. At the moment, I am using HttpWebRequest/HttpWebResponse to make the calls. The problem is that HttpWebRequest throws an exception on 4xx (and 5xx) codes. I am catching the exception, and then (usually) sending another request based on the data in the 4xx message (e.g., try POST, then MERGE in a failure).
This works, but having to recreate the TCP/HTTP connection every time is expensive. What I want to be able to do is a get a response stream or object that has the response data, but does not throw an exception, allowing me to make another request on the connection (via keep-alive). This should reduce the connection overhead, and boost performance.
One way I have looked at is using the TcpClient directly, but that is a headache, as I then have to write code to support chunked data, encodings, proxies, etc. I am hoping to avoid this.
Is there a way to prevent WebExceptions using the System.Net HTTP client? If not, are there any other options (hopefully open source)? Any suggestions are very welcome.
Not sure why your HTTP connection would be getting closed due to an exception, however, if you use the HttpClient from here http://nuget.org/List/Packages/HttpClient it does not throw an exception on a 400.