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.
Asked
Active
Viewed 142 times
-1
-
So your question isn't how to suppress `WebException`, it's actually asking if there's a way to get the response status code before the exception is thrown, right? – ProgrammingLlama Oct 08 '20 at 01:40
-
The performance impact of an exception being thrown is only really noticeable with a debugger attached. – Etienne de Martel Oct 08 '20 at 02:08
1 Answers
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