xamarin.androidhttpclientsslexception

HttpClient.PostAsync throw Javax.Net.Ssl.SSLException


httpclient PostAysnc method sometimes throws Javax.Net.Ssl.SSLException. I am sure the endpoint url and httpContent are valid. This kind of exception only happens in very low frequency.

The message of the exception is "Read error: ssl=0x716ae3b788: SSL_ERROR_WANT_READ occured. You should never see this."

Project environment: Xamarin.Android with NETStandard.Library 2.0.3 Android build configuration: HttpClient implementation: "AndroidClientHandler" SSL/TLS implementation: "Native TLS 1.2+"

Below is the detail stack traces:

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

Source code: "await _httpClient.PostAsync(endPoint, httpContent)" is the line which throws the exception.

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();
    }
}

Solution

  • I think I have figured out the reason and solution for this exception. The reason is that Android AndroidClientHandler throws the Java exceptions during the handling of the HTTP requests. (However, iOS Handler will throw .NET HttpRequestException or WebException). These exceptions normally happen when the network is not stable and should be fine if we try it again. For my case as I have Polly policies applied, I just catch all the exception and do the exception type name by string comparison. Reference regarding android handler: github.com/xamarin/xamarin-android/issues/3216