HttpClient.PostAsync 扔 Javax.Net.Ssl.SSLException

HttpClient.PostAsync throw Javax.Net.Ssl.SSLException

httpclient PostAysnc 方法有时会抛出 Javax.Net.Ssl.SSLException。 我确定端点 url 和 httpContent 是有效的。这种异常只出现在很低的频率。

异常信息为"Read error: ssl=0x716ae3b788: SSL_ERROR_WANT_READ occured. You should never see this."

项目环境:Xamarin.Android with NETStandard.Library 2.0.3 Android 构建配置: HttpClient 实现:"AndroidClientHandler" SSL/TLS 实施:"Native TLS 1.2+"

下面是详细的堆栈跟踪:

Java.Interop
JniEnvironment+InstanceMethods.CallIntMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args)
Java.Interop
JniPeerMembers+JniInstanceMethods.InvokeVirtualInt32Method (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters)
Java.Net
HttpURLConnection.get_ResponseCode ()
Xamarin.Android.Net
AndroidClientHandler+<>c__DisplayClass46_0.<DoProcessRequest>b__1 ()
System.Threading.Tasks
Task`1[TResult].InnerInvoke ()
System.Threading.Tasks
Task.Execute ()
Xamarin.Android.Net
AndroidClientHandler.DoProcessRequest (System.Net.Http.HttpRequestMessage request, Java.Net.URL javaUrl, Java.Net.HttpURLConnection httpConnection, System.Threading.CancellationToken cancellationToken, Xamarin.Android.Net.AndroidClientHandler+RequestRedirectionState redirectState)
Xamarin.Android.Net
AndroidClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
System.Net.Http
HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken)
UbiParkLib.Services.ApiCommon
APIBaseService.Post (System.String endPoint, System.Object payload) /Users/kevinhu/Source/UbiParkApp/UbiParkApp/UbiParkLib/Services/ApiCommon/APIBaseService.cs:371

源代码: "await _httpClient.PostAsync(endPoint, httpContent)" 是抛出异常的行。

using (HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json"))
{
    using (var response = await _httpClient.PostAsync(endPoint, httpContent))
    {
        response.EnsureSuccessStatusCode();
        result = await response.Content.ReadAsStringAsync();
    }
}

我想我已经找到这个异常的原因和解决方法了。原因是 Android AndroidClientHandler 在处理 HTTP 请求期间抛出 Java 异常。 (但是,iOS 处理程序将抛出 .NET HttpRequestException 或 WebException)。这些异常通常发生在网络不稳定的时候,如果我们再试一次应该没问题。对于我的情况,因为我应用了 Polly 策略,我只捕获所有异常并通过字符串比较来处理异常类型名称。关于 android 处理程序的参考:github.com/xamarin/xamarin-android/issues/3216