Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix a "java.io.IOException: unexpected end of stream on Connection" exception in HTTPClient on .NetStandard

I have been scratching my head with this one for a while. I'm building an Android app in Xamarin and I have a login POST request that mostly works, but will occasionally recieve this error. I notify the user and tell them to try again but I am still getting the error far too often and want to fix it so the app provides a smoother experience.

Here is the stack trace I have logged on App Center:

LoginProvider+d__1.MoveNext () C:\source\repos{MyApp}{MyApp}{MyApp}\Services\LoginProvider.cs:35
java.io.IOException: unexpected end of stream on Connection{testclarity.i-menzies.com:443, proxy=DIRECT@ hostAddress=62.244.173.166 cipherSuite=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 protocol=http/1.1} (recycle count=0)
com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:210)
com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:905)
com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:789)
com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:443)
com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:388)
com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:501)
com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
Caused by: java.io.EOFException: \n not found: size=0 content=...
com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:200)
com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)

I am using .NETStandard 2.0, with Xamarin Forms 3.2.0.871581.

I have scoured most of Google and identified that this is an issue with various Android libraries, especially OkHttp (the same one mentioned in my stack trace). I've tried investigating the .NETStandard source on Github to identify the possible cause, but I find the project very difficult to navigate, especially as this problem seems to be Android specific. Any advice on finding the right source would be ideal.

Things I have tried based on suggestions from the internet:

  • Setting my connection header to closed.
  • Setting my transfer encoding to chunked.
  • Swapping out the Android HttpClient implementation from Android to Default in the Android project's properties.

These seem to be among the popular suggestions online, some of which work for people, some of which don't.

Another common suggestion is setting the OkHttp library's configuration to OkHTTP.setRetryOnConnectionFailure(true), which apparently fixes the problem for many people, as suggested here: https://github.com/square/okhttp/issues/1517#issuecomment-144069139.

Also, a similar bug seems to have been filed in Xamarin.Android here: https://bugzilla.xamarin.com/show_bug.cgi?id=41100. But this has been marked as fixed. I'm not sure whether this would feed into my Xamarin Forms project.

Does anyone know how I can fix this problem or how I can investigate further beyond what I've already tried?

like image 888
Chucky Avatar asked Jan 22 '19 12:01

Chucky


2 Answers

After following the information in this link: https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/http-stack?tabs=macos

And changing my HTTPHandler to the native android handler:

The Xamarin.Android HttpClient configuration is in Project Options > Android Options, then click the Advanced Options button.

These are the recommended settings for TLS 1.2 support:

Visual Studio Android Options

enter image description here

The one extra bit I missed was:

Projects must reference the System.Net.Http assembly.

Make sure your project references System.Net.Http otherwise it will still use OKHttp

like image 88
User1 Avatar answered Sep 29 '22 02:09

User1


You can just go the Android Project > Properties > AssemblyInfo.cs file. Open the AssemblyInfo file and add the following line at the end:

[assembly: Application(UsesCleartextTraffic = true)]

This helped me solve my problem. I hope it helps you also

like image 45
Simbarashe Mupfururirwa Avatar answered Sep 29 '22 03:09

Simbarashe Mupfururirwa