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

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

问题描述

一段时间以来,我一直在用这种挠头。我正在Xamarin中构建一个Android应用,并且我有一个登录POST请求,该请求通常可以运行,但偶尔会收到此错误。我通知用户并告诉他们再试一次,但我仍然经常收到错误消息,并希望对其进行修复,以便该应用程序提供更流畅的体验。



这里是我登录到App Center的堆栈跟踪:

  LoginProvider + d__1.MoveNext()C:\source\repos { MyApp} {MyApp} {MyApp} \服务\LoginProvider.cs:35 
java.io.IOException:Connection {testclarity.i-menzies.com:443,proxy = DIRECT @ hostAddress上流的意外结束= 62.244.173.166 cipherSuite = TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256协议= 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.execu te(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:找不到n:size = 0 content = ...
com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java :200)
com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)

我正在使用带有Xamarin Forms 3.2.0.871581的.NETStandard 2.0。



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



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




  • 将我的连接头设置为关闭。

  • 将我的传输编码
    设置为分块。

  • 在Android项目的属性中将Android HttpClient实现从
    Android替换为Default。



这些似乎是在线上流行的建议之一,其中有些建议对人们有用,而有些建议则无效。



另一个常见的建议是将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\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?

解决方案

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:Connection上的流意外结束” .NetStandard上的HTTPClient中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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