记一次使用Okhttp遇到的坑!!!
2016-08-17 17:47
344 查看
关于Okhttp的介绍以及牛逼之处笔者不进行科普了,不懂的自行戳http://blog.csdn.net/lmj623565791/article/details/47911083
在使用Okhttp的过程中频繁的发起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"),
即增加关闭连接,不让它保持连接。后来自己测试一番貌似再没有出现过上面的问题了。
不加头部时Connection的值为Keep-Alive
增加Connection=false后头部如下:
关于Http头 Connection=close的作用:
在http1.1中request和reponse header中都有可能出现一个connection头字段,此header的含义是当client和server通信时对于长链接如何进行处理。
在http1.1中,client和server都是默认对方支持长链接的, 如果client使用http1.1协议,但又不希望使用长链接,则需要在header中指明connection的值为close;如果server方也不想支持长链接,则在response中也需要明确说明connection的值为close.
不论request还是response的header中包含了值为close的connection,都表明当前正在使用的tcp链接在请求处理完毕后会被断掉。以后client再进行新的请求时就必须创建新的tcp链接了。 HTTP Connection的 close设置允许客户端或服务器中任何一方关闭底层的连接双方都会要求在处理请求后关闭它们的TCP连接。
在使用Okhttp的过程中频繁的发起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"),
即增加关闭连接,不让它保持连接。后来自己测试一番貌似再没有出现过上面的问题了。
不加头部时Connection的值为Keep-Alive
增加Connection=false后头部如下:
关于Http头 Connection=close的作用:
在http1.1中request和reponse header中都有可能出现一个connection头字段,此header的含义是当client和server通信时对于长链接如何进行处理。
在http1.1中,client和server都是默认对方支持长链接的, 如果client使用http1.1协议,但又不希望使用长链接,则需要在header中指明connection的值为close;如果server方也不想支持长链接,则在response中也需要明确说明connection的值为close.
不论request还是response的header中包含了值为close的connection,都表明当前正在使用的tcp链接在请求处理完毕后会被断掉。以后client再进行新的请求时就必须创建新的tcp链接了。 HTTP Connection的 close设置允许客户端或服务器中任何一方关闭底层的连接双方都会要求在处理请求后关闭它们的TCP连接。
相关文章推荐
- 遇到的问题---Windows socket error: 通常每个套接字地址(协议/网络地址/端口)只允许使用一次
- 使用Bmob时遇到的Android依赖包冲突(Gson、Okhttp)
- 【Richie Zhu】android开发时使用Android模拟器经常遇到连不上、连一次掉一次等诸多问题的解决方案
- 记一次RecyclerView使用过程遇到的坑
- Windows下安装Apache HTTP Server 无法启动遇到OS10048 通常每个套接字地址(协议/网络地址/端口)只允许使用一次 的问题解决方法
- 使用okhttp遇到的问题,简单记录
- Android -- 使用OKhttp获取response时遇到的坑
- 记录一次使用jiaThis社会化分享组件遇到的坑!!
- 使用OkHttp3遇到的一些坑
- 遇到的问题---Windows socket error: 通常每个套接字地址(协议/网络地址/端口)只允许使用一次
- Android 使用OKhttp获取response时遇到的坑
- eclipse使用okhttp时添加jar包时遇到的坑
- 使用Data Access Application Block遇到的问题
- 使用Wallop 遇到的问题
- VC6使用过程中遇到的问题收集(1)
- 如何使用bat文件一次运行多个程序阿?
- 使用Access存储Session,遇到了SessionId问题?
- SimpleDateFormt 使用中遇到的一个小问题
- 求解在SQL中使用了where列所遇到的问题
- 使用vs.net遇到的问题