title: How to fix a "java.io.IOException: unexpected end of stream on Connection" exception in HTTPClient on .NetStandard
body: <p>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.</p>
  
  <p>Here is the stack trace I have logged on App Center:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I am using .NETStandard 2.0, with Xamarin Forms 3.2.0.871581.</p>
  
  <p>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. <strong>Any advice on finding the right source would be ideal.</strong> </p>
  
  <p>Things I have tried based on suggestions from the internet:</p>
  
  <ul>
  <li>Setting my connection header to closed. </li>
  <li>Setting my transfer encoding
  to chunked. </li>
  <li>Swapping out the Android HttpClient implementation from
  Android to Default in the Android project's properties.</li>
  </ul>
  
  <p>These seem to be among the popular suggestions online, some of which work for people, some of which don't. </p>
  
  <p>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: <a href="https://github.com/square/okhttp/issues/1517#issuecomment-144069139" rel="noreferrer">https://github.com/square/okhttp/issues/1517#issuecomment-144069139</a>.</p>
  
  <p>Also, a similar bug seems to have been filed in Xamarin.Android here: <a href="https://bugzilla.xamarin.com/show_bug.cgi?id=41100" rel="noreferrer">https://bugzilla.xamarin.com/show_bug.cgi?id=41100</a>. But this has been marked as fixed. I'm not sure whether this would feed into my Xamarin Forms project.</p>
  
  <p>Does anyone know how I can fix this problem or how I can investigate further beyond what I've already tried?</p>