Xamarin-android: Android HttpClient implementation in .NET Standard Project throwing Java Exception

Created on 13 Jun 2019  路  19Comments  路  Source: xamarin/xamarin-android

Steps to Reproduce

  1. Set the AndroidHttpClientHandlerType to Xamarin.Android.Net.AndroidClientHandler
    image

  2. Replicate poor internet or no internet case using emulator / device

Different exception thrown will be like

"type": "Java.IO.IOException", "message": "Read error: ssl=0x7f83263840: Failure in SSL library, usually a protocol error\nerror:100000d7:SSL routines:OPENSSL_internal:SSL_HANDSHAKE_FAILURE (external/boringssl/src/ssl/s3_pkt.c:402 0x7f9f35fe7e:0x00000000)", "stackTrace": " at Java.Interop.JniEnvironment+InstanceMethods.CallIntMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <6daf52cf652e4e32ad801c3e8a5bbdb4>:0 \n at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualInt32Method (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) <0x7f90edfc54 + 0x0008f> in <6daf52cf652e4e32ad801c3e8a5bbdb4>:0 \n at Java.Net.HttpURLConnection.get_ResponseCode () <0x7f8867b88c + 0x00057> in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass45_0.<DoProcessRequest>b__1 () <0x7f884f5d10 + 0x00017> in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <ddff213794e94e648b95c7facad6817d>:0 \n at System.Threading.Tasks.Task.Execute () <0x7f890417d8 + 0x0001f> in <ddff213794e94e648b95c7facad6817d>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at Xamarin.Android.Net.AndroidClientHandler+<DoProcessRequest>d__45.MoveNext () [0x0036c] in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at Xamarin.Android.Net.AndroidClientHandler+<SendAsync>d__40.MoveNext () <0x7f884f53b0 + 0x0028f> in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at System.Net.Http.HttpClient+<SendAsyncWorker>d__49.MoveNext () [0x000ca] in <b42ca876d5974c1cba625d13c503e2d8>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at apiurl.Services.CommonFiles.Services.RequestProvider+<PostAsync>d__7`2[TRequest,TResult].MoveNext () [0x001d7] in <debda5445b0b406da295bfd4eb7b720e>:0 \n --- End of managed Java.IO.IOException stack trace ---\njavax.net.ssl.SSLProtocolException: Read error: ssl=0x7f83263840: Failure in SSL library, usually a protocol error\nerror:100000d7:SSL routines:OPENSSL_internal:SSL_HANDSHAKE_FAILURE (external/boringssl/src/ssl/s3_pkt.c:402 0x7f9f35fe7e:0x00000000)\n\tat com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)\n\tat com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:758)\n\tat com.android.okhttp.okio.Okio$2.read(Okio.java:136)\n\tat com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)\n\tat com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)\n\tat com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)\n\tat com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)\n\tat com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)\n\tat com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)\n\tat com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:907)\n\tat com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:783)\n\tat com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:463)\n\tat com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405)\n\tat com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:521)\n\tat com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)\n\tat com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java)\n", 
"type": "Java.Lang.Exception", "message": "isConnected failed: ETIMEDOUT (Connection timed out)", "stackTrace": "\n --- End of managed Java.Lang.Exception stack trace ---\nandroid.system.ErrnoException: isConnected failed: ETIMEDOUT (Connection timed out)\n\tat libcore.io.IoBridge.isConnected(IoBridge.java:223)\n\tat libcore.io.IoBridge.connectErrno(IoBridge.java:171)\n\tat libcore.io.IoBridge.connect(IoBridge.java:122)\n\tat java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)\n\tat java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)\n\tat java.net.Socket.connect(Socket.java:882)\n\tat com.android.okhttp.internal.Platform.connectSocket(Platform.java:174)\n\tat com.android.okhttp.Connection.connect(Connection.java:1222)\n\tat com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:395)\n\tat com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:298)\n\tat com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:399)\n\tat com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:110)\n\tat com.android.okhttp.internal.http.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)\n\tat com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:25)\n", 
"type": "Java.Net.SocketException", "message": "Socket closed", "stackTrace": " at Java.Interop.JniEnvironment+InstanceMethods.CallIntMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <6daf52cf652e4e32ad801c3e8a5bbdb4>:0 \n at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualInt32Method (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) <0x7e81957c54 + 0x0008f> in <6daf52cf652e4e32ad801c3e8a5bbdb4>:0 \n at Java.Net.HttpURLConnection.get_ResponseCode () <0x7e80a5988c + 0x00057> in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass45_0.<DoProcessRequest>b__1 () <0x7e808d3d10 + 0x00017> in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <ddff213794e94e648b95c7facad6817d>:0 \n at System.Threading.Tasks.Task.Execute () <0x7e81c367d8 + 0x0001f> in <ddff213794e94e648b95c7facad6817d>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at Xamarin.Android.Net.AndroidClientHandler+<DoProcessRequest>d__45.MoveNext () [0x0036c] in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at Xamarin.Android.Net.AndroidClientHandler+<SendAsync>d__40.MoveNext () <0x7e808d33b0 + 0x0028f> in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at System.Net.Http.HttpClient+<SendAsyncWorker>d__49.MoveNext () [0x000ca] in <b42ca876d5974c1cba625d13c503e2d8>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at apiurl.Services.CommonFiles.Services.RequestProvider+<PostAsync>d__7`2[TRequest,TResult].MoveNext () [0x001d7] in <debda5445b0b406da295bfd4eb7b720e>:0 \n --- End of managed Java.Net.SocketException stack trace ---\njava.net.SocketException: Socket closed\n\tat com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)\n\tat com.android.org.conscrypt.NativeSsl.read(NativeSsl.java:399)\n\tat com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:546)\n\tat com.android.okhttp.okio.Okio$2.read(Okio.java:136)\n\tat com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)\n\tat com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)\n\tat com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)\n\tat com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)\n\tat com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186)\n\tat com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)\n\tat com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737)\n\tat com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:609)\n\tat com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:471)\n\tat com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)\n\tat com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538)\n\tat com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)\n\tat com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:26)\n", 
 "type": "Java.IO.IOException", "message": "Read error: ssl=0x7cd0918140: I/O error during system call, Software caused connection abort", "stackTrace": " at Java.Interop.JniEnvironment+InstanceMethods.CallIntMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <6daf52cf652e4e32ad801c3e8a5bbdb4>:0 \n at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualInt32Method (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) <0x7cd7065c54 + 0x0008f> in <6daf52cf652e4e32ad801c3e8a5bbdb4>:0 \n at Java.Net.HttpURLConnection.get_ResponseCode () <0x7cd725b88c + 0x00057> in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass45_0.<DoProcessRequest>b__1 () <0x7cd70d5d10 + 0x00017> in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <ddff213794e94e648b95c7facad6817d>:0 \n at System.Threading.Tasks.Task.Execute () <0x7cd84417d8 + 0x0001f> in <ddff213794e94e648b95c7facad6817d>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at Xamarin.Android.Net.AndroidClientHandler+<DoProcessRequest>d__45.MoveNext () [0x0036c] in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at Xamarin.Android.Net.AndroidClientHandler+<SendAsync>d__40.MoveNext () <0x7cd70d53b0 + 0x0028f> in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at System.Net.Http.HttpClient+<SendAsyncWorker>d__49.MoveNext () [0x000ca] in <b42ca876d5974c1cba625d13c503e2d8>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at apiurl.Services.CommonFiles.Services.RequestProvider+<PostAsync>d__7`2[TRequest,TResult].MoveNext () [0x001d7] in <debda5445b0b406da295bfd4eb7b720e>:0 \n --- End of managed Java.IO.IOException stack trace ---\njavax.net.ssl.SSLException: Read error: ssl=0x7cd0918140: I/O error during system call, Software caused connection abort\n\tat com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)\n\tat com.android.org.conscrypt.SslWrapper.read(SslWrapper.java:384)\n\tat com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:541)\n\tat com.android.okhttp.okio.Okio$2.read(Okio.java:136)\n\tat com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)\n\tat com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)\n\tat com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)\n\tat com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)\n\tat com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186)\n\tat com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)\n\tat com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737)\n\tat com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:609)\n\tat com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:471)\n\tat com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)\n\tat com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538)\n\tat com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)\n\tat com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(Unknown Source:0)\n", 
 "type": "Java.IO.IOException", "message": "failed to connect to app.apiurl.com/216.58.196.211 (port 443) after 120000ms: isConnected failed: ETIMEDOUT (Connection timed out)", "stackTrace": " at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <e2b68ac502ab4e7ab5d22a38be5cc933>:0 \n at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) <0xa101868c + 0x0007b> in <e2b68ac502ab4e7ab5d22a38be5cc933>:0 \n at Javax.Net.Ssl.HttpsURLConnectionInvoker.Connect () <0x99d7cd30 + 0x00043> in <129efd51243441d5904e9ed64552d621>:0 \n at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass43_0.<ConnectAsync>b__0 () [0x0005a] in <129efd51243441d5904e9ed64552d621>:0 \n at System.Threading.Tasks.Task.InnerInvoke () <0x9a85e698 + 0x0005b> in <a6d2ea4bf3b740e994c1e13d1c003ca7>:0 \n at System.Threading.Tasks.Task.Execute () <0x9a85e438 + 0x00017> in <a6d2ea4bf3b740e994c1e13d1c003ca7>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at Xamarin.Android.Net.AndroidClientHandler+<DoProcessRequest>d__45.MoveNext () [0x0012e] in <129efd51243441d5904e9ed64552d621>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at Xamarin.Android.Net.AndroidClientHandler+<SendAsync>d__40.MoveNext () <0x99d65054 + 0x0033b> in <129efd51243441d5904e9ed64552d621>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at System.Net.Http.HttpClient+<SendAsyncWorker>d__49.MoveNext () [0x000ca] in <13aaea54b2f14535b37e703fae949c7a>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at apiurl.Services.CommonFiles.Services.RequestProvider+<PostAsync>d__7`2[TRequest,TResult].MoveNext () [0x001d7] in <f54fcf91dad54864b43a249aeed3b0a7>:0 \n --- End of managed Java.IO.IOException stack trace ---\njava.net.SocketTimeoutException: failed to connect to app.apiurl.com/216.58.196.211 (port 443) after 120000ms: isConnected failed: ETIMEDOUT (Connection timed out)\n\tat libcore.io.IoBridge.isConnected(IoBridge.java:236)\n\tat libcore.io.IoBridge.connectErrno(IoBridge.java:171)\n\tat libcore.io.IoBridge.connect(IoBridge.java:122)\n\tat java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)\n\tat java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)\n\tat java.net.Socket.connect(Socket.java:882)\n\tat com.android.okhttp.internal.Platform.connectSocket(Platform.java:174)\n\tat com.android.okhttp.Connection.connect(Connection.java:1222)\n\tat com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:395)\n\tat com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:298)\n\tat com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:399)\n\tat com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:110)\n\tat com.android.okhttp.internal.http.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)\n\tat com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:25)\nCaused by: android.system.ErrnoException: isConnected failed: ETIMEDOUT (Connection timed out)\n\tat libcore.io.IoBridge.isConnected(IoBridge.java:223)\n\t... 13 more\n", 
"type": "Java.IO.IOException", "message": "Read error: ssl=0x7f516040: I/O error during system call, Connection reset by peer", "stackTrace": " at Java.Interop.JniEnvironment+InstanceMethods.CallIntMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <6daf52cf652e4e32ad801c3e8a5bbdb4>:0 \n at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualInt32Method (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) <0x855971dc + 0x0009b> in <6daf52cf652e4e32ad801c3e8a5bbdb4>:0 \n at Java.Net.HttpURLConnection.get_ResponseCode () <0x837eae9c + 0x00043> in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass45_0.<DoProcessRequest>b__1 () <0x83624f1c + 0x00013> in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <ddff213794e94e648b95c7facad6817d>:0 \n at System.Threading.Tasks.Task.Execute () <0x8445e438 + 0x00017> in <ddff213794e94e648b95c7facad6817d>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at Xamarin.Android.Net.AndroidClientHandler+<DoProcessRequest>d__45.MoveNext () [0x0036c] in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at Xamarin.Android.Net.AndroidClientHandler+<SendAsync>d__40.MoveNext () <0x836242f4 + 0x0033b> in <b38bd7d2b7b64973840f6a5f661d159b>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at System.Net.Http.HttpClient+<SendAsyncWorker>d__49.MoveNext () [0x000ca] in <b42ca876d5974c1cba625d13c503e2d8>:0 \n--- End of stack trace from previous location where exception was thrown ---\n at apiurl.Services.CommonFiles.Services.RequestProvider+<PostAsync>d__7`2[TRequest,TResult].MoveNext () [0x001d7] in <debda5445b0b406da295bfd4eb7b720e>:0 \n --- End of managed Java.IO.IOException stack trace ---\njavax.net.ssl.SSLException: Read error: ssl=0x7f516040: I/O error during system call, Connection reset by peer\n\tat com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)\n\tat com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:789)\n\tat com.android.okhttp.okio.Okio$2.read(Okio.java:141)\n\tat com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)\n\tat com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:316)\n\tat com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:310)\n\tat com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:206)\n\tat com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:239)\n\tat com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:104)\n\tat com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:1156)\n\tat com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:976)\n\tat com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:509)\n\tat com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:438)\n\tat com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:567)\n\tat com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)\n\tat com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java)\n", 

These are few samples of exception which we get in real devices.

Expected Behavior

Throw HttpRequestException or similar exception which can be handled in .NETStandard / Shared code without reference to Java.

Actual Behavior

Java.foo.Exception occurs which can't be handled without ToString or equivalent which are too "anti pattern"

Version Information

Xamarin.Andoid 9.2
Mono 5.18

Related Issue #2651

App Runtime enhancement

Most helpful comment

I'm facing the same issue. On iOS I catch a WebException while in Android I'm getting a Java.Net.UnknownHostException. That's something I can't handle on core project.

Definitively we should be getting the same exception as we do on iOS
Maybe it's just a question of adding the exception type evaluation on this catch so you also wrap Java.Net.UnknownHostException into WebException too?

All 19 comments

Exception when used Managed HttpClientHandler (which we can intercept and handle)

{System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: The operation has timed out.
  at System.Net.HttpWebRequest.RunWithTimeoutWorker[T] (System.Threading.Tasks.Task`1[TResult] workerTask, System.Int32 timeout, System.Action abort, System.Func`1[TResult] aborted, System.Threading.CancellationTokenSource cts) [0x000f8] in <74989b9daab94528ac2c4b7da235b9c5>:0 
  at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x00020] in <74989b9daab94528ac2c4b7da235b9c5>:0 
  at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x0000f] in <3a61d48d66dd458fa0b64990b6c8f8cc>:0 
--- End of stack trace from previous location where exception was thrown ---

  at System.Net.Http.HttpClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x0041d] in <162fad71ed6e4a3cb7598d196d170e1b>:0 
   --- End of inner exception stack trace ---
  at System.Net.Http.HttpClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00478] in <162fad71ed6e4a3cb7598d196d170e1b>:0 
  at System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) [0x000ca] in <162fad71ed6e4a3cb7598d196d170e1b>:0 
  at SpotMyCourt.Services.CommonFiles.Services.RequestProvider.GetAsync[TResult] (System.String uri, System.String token) [0x000b3] in 52 }

@jonathanpeppers from #2651

@muhaym it seems that if you want to deal with HttpRequestExceptions you should use the Managed HttpClientHandler, is there a reason you cannot use it?

@gugavaro
https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/http-stack?tabs=windows
When going through Cons:

  • It is not fully integrated with the OS (eg. limited to TLS 1.0).
  • It is usually much slower (eg. encryption) than native API.
  • It requires more managed code, creating larger applications.

Larger application and slower performance is a big challenge in Android.

And Pros of Native stack with Performance key in every application. This is why we cannot use Managed HttpClientHandler.

@grendello you might want to weigh in.

The mismatch here is that iOS's NSUrlSessionHandler maps to WebException: https://github.com/xamarin/xamarin-macios/blob/fb8db957645d4ea4067e18ea5938f48c548a7bb6/src/Foundation/NSUrlSessionHandler.cs#L319

And you can actually handle these in netstandard libraries, while the only way with AndroidClientHandler is to catch (Exception e) and check the type of the exception with a string comparison...

I don't know if there is an easy fix for us, but worth considering.

I'm facing the same issue. On iOS I catch a WebException while in Android I'm getting a Java.Net.UnknownHostException. That's something I can't handle on core project.

Definitively we should be getting the same exception as we do on iOS
Maybe it's just a question of adding the exception type evaluation on this catch so you also wrap Java.Net.UnknownHostException into WebException too?

am facing the same issue with Java.Net.ConnectException any update?

I agree that this is a pretty bad problem. Part of the implied contract for any implementation of HttpMessageHandler is that it'll behave the way .NET code in general would expect it to behave, including throwing the same kinds of exceptions you would normally see in .NET, not platform-specific ones. And based on the code that @claudioredi linked to, it seems like the intention was for it to do so, but they just didn't quite implement it right.

We're facing the same type of Exceptions :( any workaround guys?

Adding my vote on this issue, been dealing with it for years. On a past project I believe we worked around this by creating our own subclass of AndroidClientHandler in our Android project, with an override of SendAsync that catches Java exceptions and maps them to .NET exceptions.

@StefAnglr Could you please provide the source?

@ysmoradi Unfortunately, I no longer work for that company and do not have access to the source 馃槗 Sorry!

@ysmoradi There is a good example of this approach in the latest comment on #2651 - just pass an instance of WorkaroundAndroidClientHandler to the HttpClient constructor.

So. Uhm. Is this still under consideration or should we keep hacking our Exception handlers to check the type of the exception based on class name?

@Cheesebaron a new Java exception feature is in the latest preview: https://docs.microsoft.com/en-us/xamarin/android/release-notes/10/10.2#option-for-net-exceptions-from-certain-android-apis

You have to opt into it, because it is a breaking change.

@brendanzagaeski I know you tried this out, is this issue something we can close now?

Interesting. This looks like a slightly separate case from the set of changes that is included so far in the Xamarin.Android 10.2 Preview. Those changes so far didn't add any uses of WebException or HttpRequestException: https://github.com/xamarin/xamarin-android/commit/60363ef45cd6994a322adac1b73d4271b0658c1e.

This AndroidClientHandler case was probably unintentionally overlooked because AndroidClientHandler isn't a binding for an Android class.

As I understand it, the set of changes that's in the current preview hasn't yet fulfilled all of the goals for the feature, so this AndroidClientHandler scenario sounds like another good one to add to the list for the next round of revisions on that feature.

I'll add a cross-referencing note to this issue on the other issue I filed that's tracking some changes for that feature.

@jonathanpeppers @brendanzagaeski That msbuild prop looks pretty cool. Looking forward to give it a spin!

Should we still be using the ModernHttpClient? With the NativeMessageHandler...

@wesoos considering that it hasn't been maintained in 4 years and the HttpClient handlers Xamarin do the same thing, just better. I'd say, no!

Was this page helpful?
0 / 5 - 0 ratings