Runtime: Can't reliably determine if connection was successful when using HttpClient

Created on 19 Sep 2019  路  5Comments  路  Source: dotnet/runtime

If an exception is thrown from a call to HttpClient.SendAsync( ) there's no way to be certain that the connection itself failed, vs. e.g., a timeout before trying to read the response.
While there are certain SocketException SocketError values that can only happen if the DNS lookup or connection fails, if there is a) a failure negotiating the SSL handshake or b) a timeout trying to connect, there's no reliable way to programmatically distinguish this from other possible errors.
This is important in many real world applications where if there's any possibility that the remote server did receive all or part of the request, but a network or system failure occurred after this point, it's necessary to attempt to contact that server again to determine whether it did process the request.
If the HttpClient had a way of determine how many bytes had been successfully sent to the remote server before the exception occurred, that would solve it nicely too.

area-System.Net.Http enhancement

Most helpful comment

This is similar to dotnet/runtime#15567 and related issues. Basically we need to enhance the top-level HttpRequestException and add a property that would be similar to the WebException.Status property. That property was of type WebExceptionStatus enum and made it easy to distinguish various error conditions.

All 5 comments

This is similar to dotnet/runtime#15567 and related issues. Basically we need to enhance the top-level HttpRequestException and add a property that would be similar to the WebException.Status property. That property was of type WebExceptionStatus enum and made it easy to distinguish various error conditions.

This feels related to dotnet/runtime#21965. A solution here should probably also address that issue.

I notice that mentions OperationCanceledException - in the testing I'd done I was getting OperationCanceledException when the connection timed out, but just a regular TaskCanceledException when it timed out trying to read a response. Not really something I could rely on either way!

I have the same error.But I publish the demo on ubuntu across docker.
The base image is microsoft/dotnet:2.2-aspnetcore-runtime.
when i use the httpclient request the remote api,sometimes work right ,but somethimes it get wrong with the error message:
the operation was canceled

but it doesn't occur on window environment锛侊紒
somebody have the same question with me?

Duplicate of dotnet/runtime#15567

Was this page helpful?
0 / 5 - 0 ratings