android用okhttp的坑之java.io.IOException: unexpected end of stream on okhttp3.Address@178de5cc!

在使用Okhttp的過程中頻繁的發(fā)起Http請求時偶爾會看到如下的錯誤

ERROR [IOException]-[120]

java.io.IOException: unexpected end of stream on okhttp3.Address@178de5cc

at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:201)

at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)

at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:53)

at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)

at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)

at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)

at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)

at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109)

at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)

at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)

at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)

at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)

at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)

at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)

at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)

at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)

at okhttp3.RealCall.access$100(RealCall.java:33)

at okhttp3.RealCall$AsyncCall.execute(RealCall.java:120)

at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

at java.lang.Thread.run(Thread.java:841)

Caused by: java.io.EOFException: \n not found: size=0 content=…

at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:215)

at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:186)

... 21 more

不知是Okhttp的一個bug還是什么奇妙的原因。功夫不負有心人苦逼的找了一個下午,終于找到了一個解決方案,就是在http的頭部添加addHeader("Connection", "close"),

即增加關(guān)閉連接,不讓它保持連接。后來自己測試一番貌似再沒有出現(xiàn)過上面的問題了。

不加頭部時Connection的值為Keep-Alive

增加Connection=false后頭部如下:

關(guān)于Http頭 Connection=close的作用:

在http1.1中request和reponse header中都有可能出現(xiàn)一個connection頭字段,此header的含義是當client和server通信時對于長鏈接如何進行處理。

在http1.1中,client和server都是默認對方支持長鏈接的, 如果client使用http1.1協(xié)議,但又不希望使用長鏈接,則需要在header中指明connection的值為close;如果server方也不想支持長鏈接,則在response中也需要明確說明connection的值為close.

不論request還是response的header中包含了值為close的connection,都表明當前正在使用的tcp鏈接在請求處理完畢后會被斷掉。以后client再進行新的請求時就必須創(chuàng)建新的tcp鏈接了。 HTTP Connection的 close設(shè)置允許客戶端或服務(wù)器中任何一方關(guān)閉底層的連接雙方都會要求在處理請求后關(guān)閉它們的TCP連接。


下面是addHeader的正確的方式:

class ? NetInterceptor ? implements ? Interceptor {

@Override

public Responseintercept(Chain chain)throws IOException {

Request request = chain.request().newBuilder()

.addHeader("Connection","close").build();

returnchain.proceed(request);

}

}


OkHttpClient client =newOkHttpClient.Builder()

.addNetworkInterceptor(new NetInterceptor())

.build();

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 簡介 目前在HTTP協(xié)議請求庫中,OKHttp應(yīng)當是非?;鸬?,使用也非常的簡單。網(wǎng)上有很多文章寫了關(guān)于OkHttp...
    第八區(qū)閱讀 1,446評論 1 5
  • OkHttp源碼的samples的簡單使用的示例: public static void main(String....
    _warren閱讀 879評論 0 1
  • 關(guān)于okhttp是一款優(yōu)秀的網(wǎng)絡(luò)請求框架,關(guān)于它的源碼分析文章有很多,這里分享我在學習過程中讀到的感覺比較好的文章...
    蕉下孤客閱讀 3,734評論 2 38
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,695評論 19 139
  • 現(xiàn)在生活迷茫,主要是工作沒有著落,痛苦啊。不知道自己能干什么?不太喜歡機械。。。。那個自己在行什么呢?心累了
    陽光下奔跑的孩子閱讀 114評論 0 0

友情鏈接更多精彩內(nèi)容