0

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();
    }
}
Kevin Hu
  • 80
  • 1
  • 11
  • SSL_WANT_READ means "wait for the socket to be readable, then call this function again."This exception may related to remote host, the remote host is suddenly stopped, the host is rebooted, the host or remote network interface is disabled, or the remote host uses a hard close, did you monitor your network quatity in your remote host? – Leon Aug 01 '19 at 08:37
  • Thank you @LeonLu-MSFT. Your suggestion makes a lot sense. I need talk this with my back-end engineer. What I knew is that the back-end is hosted on Microsoft Azure. – Kevin Hu Aug 01 '19 at 11:01
  • 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: https://github.com/xamarin/xamarin-android/issues/3216 – Kevin Hu Aug 02 '19 at 00:53
  • Yes, this issue related to the network not stable normally, you can post your comment to answer, and mark it as answer, it will help others who have similar issue. – Leon Aug 02 '19 at 12:11

1 Answers1

0

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

Kevin Hu
  • 80
  • 1
  • 11