Suppression of WebException

167 views Asked by At

Is there a way to suppress the occurrence of WebException when the http status code is 500 in C # WebClient? WebException is created by StackTrace, which slows down the system. This is especially serious if WebExceptions occur frequently.

1

There are 1 answers

2
gunr2171 On

There are many http clients, one of which is WebClient. WebClient is designed to throw exceptions. There is no way to have it not throw exceptions, as much as people would like.

Its predecessor, HttpWebRequest/HttpWebResponse, also does this.

If you don't want to catch the exception, you'll need to use another client.

HttpClient doesn't throw exceptions based on the returned status code, so that's an option.

You could use Flurl as an alternative which uses HttpClient under the hood. Add the error handling method .AllowHttpStatus("500").