Sam Afshari's Notes
  • GitHub
  • Twitter/X
  • All posts
  • Ed
  • NuGets
  • POIWorld
  • RedCorners

Mono Frustrations: WebRequest - Wed, Jul 1, 2015

The WebRequest class has many issues, especially in Mono. A major problem occurs when the response code is an HTTP error (e.g. 4xx), for which the class throws an Exception, causing a Runtime Error if it goes unhandled. This is not a big deal in .NET, because the request.GetResponse() call can be surrounded by a try/catch block that catches the thrown WebException, which still contains the response in its Response property. However, a bug in Mono makes it impossible to retrieve the response from a WebException, as the Response property is always null.

Solution: It is a better idea to use a TcpClient instead and manually generate an HTTP request using it.

Back to Home


© Sam Afshari 2024