如何修复“java.io.IOException:连接上的流意外结束".NetStandard 上的 HTTPClient 中的异常 [英] How to fix a "java.io.IOException: unexpected end of stream on Connection" exception in HTTPClient on .NetStandard

查看:62
本文介绍了如何修复“java.io.IOException:连接上的流意外结束".NetStandard 上的 HTTPClient 中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为这个问题挠头一段时间.我正在 Xamarin 中构建一个 Android 应用程序,并且我有一个登录 POST 请求,该请求大部分有效,但偶尔会收到此错误.我通知用户并告诉他们再试一次,但我仍然经常遇到错误,我想修复它以便应用程序提供更流畅的体验.

这是我在 App Center 上登录的堆栈跟踪:

LoginProvider+d__1.MoveNext() C:source
epos{MyApp}{MyApp}{MyApp}ServicesLoginProvider.cs:35java.io.IOException: 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}(回收次数=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)引起:java.io.EOFException: 
 not found: size=0 content=...com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:200)com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)

我使用的是 .NETStandard 2.0 和 Xamarin Forms 3.2.0.871581.

我搜索了 Google 的大部分内容,发现这是各种 Android 库的问题,尤其是 OkHttp(我的堆栈跟踪中提到的同一个).我曾尝试调查 Github 上的 .NETStandard 源代码以确定可能的原因,但我发现该项目很难导航,尤其是这个问题似乎是 Android 特有的.任何有关寻找合适来源的建议都是理想的.

我根据互联网的建议尝试过的事情:

  • 将我的连接标头设置为关闭.
  • 设置我的传输编码分块.
  • 将 Android HttpClient 实现从在 Android 项目的属性中将 Android 设为默认值.

这些似乎是网上流行的建议之一,其中一些对人们有用,而另一些则不起作用.

另一个常见的建议是将 OkHttp 库的配置设置为 OkHTTP.setRetryOnConnectionFailure(true),这显然解决了很多人的问题,如此处建议:

我遗漏的一点是:

<块引用>

项目必须引用 System.Net.Http 程序集.

确保你的项目引用了System.Net.Http,否则它仍然会使用OKHttp

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
epos{MyApp}{MyApp}{MyApp}ServicesLoginProvider.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: 
 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?

解决方案

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

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

这篇关于如何修复“java.io.IOException:连接上的流意外结束".NetStandard 上的 HTTPClient 中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆