-1

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.

kyouno
  • 49
  • 1
  • 7

1 Answers1

0

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

gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • Is there a way to see if the HttpClient or Flurl is using HttpWebRequest inside the module and is catching the exception inside the module so that the exception is not visible to the user? If exceptions are caught internally, the problem of slowing down the system when http status code 500 occurs frequently will not be solved. – kyouno Oct 08 '20 at 02:42
  • HttpClient and Flurl do not use HttpWebRequest, they use their own thing. They only throw exceptions if the connection failed. – gunr2171 Oct 08 '20 at 02:43