在使用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();