Keep-Alive与HttpURLConnection实现
2017-09-25 13:51
337 查看
http://docs.oracle.com/javase/6/docs/technotes/guides/net/http-keepalive.html中的说明:
在完成获取请求返回结果后或调用getInputStream().close(),JDK会清理连接并作为以后使用的连接缓存。不过前提是HTTP header中的Connection值不能为close,否则将被视为该连接不需要持久化。
HttpURLConnection中的disconnect() ,下面是部分源码:
在调用getInputStream().close()之后,调用disconnet()就不会关闭该连接,连接依然可以复用。只调用disconnet()的话,InputStream与连接都将被关闭。
closeIdleConnection()的源码:
要达到复用连接的话,调用getInputStream().close()就行,可以不使用disconnect()。
还有一种情况就是当请求异常时,HttpURLConnection提供了getErrorStream()方法可以帮助我们保持连接,官方文档中的例子:
When the application finishes reading the response body or when the application calls close() on the InputStream returned by URLConnection.getInputStream(), the JDK's HTTP protocol handler will try to clean up the connection and if successful, put the connection into a connection cache for reuse by future HTTP requests.
在完成获取请求返回结果后或调用getInputStream().close(),JDK会清理连接并作为以后使用的连接缓存。不过前提是HTTP header中的Connection值不能为close,否则将被视为该连接不需要持久化。
HttpURLConnection中的disconnect() ,下面是部分源码:
if (inputStream != null) { HttpClient hc = http; // un-synchronized boolean ka = hc.isKeepingAlive(); try { inputStream.close(); } catch (IOException ioe) { } // if the connection is persistent it may have been closed // or returned to the keep-alive cache. If it's been returned // to the keep-alive cache then we would like to close it // but it may have been allocated if (ka) { hc.closeIdleConnection(); } } else { // We are deliberatly being disconnected so HttpClient // should not try to resend the request no matter what stage // of the connection we are in. http.setDoNotRetry(true); http.closeServer(); }
在调用getInputStream().close()之后,调用disconnet()就不会关闭该连接,连接依然可以复用。只调用disconnet()的话,InputStream与连接都将被关闭。
closeIdleConnection()的源码:
/* * Close an idle connection to this URL (if it exists in the * cache). */ public void closeIdleConnection() { HttpClient http = (HttpClient) kac.get(url, null); if (http != null) { http.closeServer(); } }
要达到复用连接的话,调用getInputStream().close()就行,可以不使用disconnect()。
还有一种情况就是当请求异常时,HttpURLConnection提供了getErrorStream()方法可以帮助我们保持连接,官方文档中的例子:
try { URL a = new URL(args[0]); URLConnection urlc = a.openConnection(); is = conn.getInputStream(); int ret = 0; while ((ret = is.read(buf)) > 0) { processBuf(buf); } // close the inputstream is.close(); } catch (IOException e) { try { respCode = ((HttpURLConnection)conn).getResponseCode(); es = ((HttpURLConnection)conn).getErrorStream(); int ret = 0; // read the response body while ((ret = es.read(buf)) > 0) { processBuf(buf); } // close the errorstream es.close(); } catch(IOException ex) { // deal with the exception } }
相关文章推荐
- Java之HttpURLConnection的变态事: Keep-Alive
- HTTP实现长连接(TTP1.1和HTTP1.0相比较而言,最大的区别就是增加了持久连接支持Connection: keep-alive)
- HttpURLConnection实现下载功能
- Android杂谈(23)Service+BroadcastReceiver+数据库+HttpURLConnection实现断点续传(上)
- Http 中Connection: close和Connection: keep-alive有什么区别?
- HttpURLConnection-Post方法实现
- HttpUrlConnection消息发送失败重新发送实现
- HttpUrlConnection底层实现和关于java host绑定ip即时生效的设置及分析
- android中使用HttpURLConnection实现多线程下载
- HttpURLConnection实现文件下载
- Android杂谈(24)Service+BroadcastReceiver+数据库+HttpURLConnection实现断点续传(下)
- 利用HttpUrlConnection 上传 接收文件的实现方法
- HttpURLConnection-Get方法实现
- Android开发_使用HttpURLConnection实现多线程下载
- HttpUrlConnection底层实现和关于java host绑定ip即时生效的设置及分析
- 实现单一ListView条目样式HttpURLConnection、AsyncTask、Universal-Image-Loade
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- javaHTTP请求工具类-使用HttpURLConnection实现
- 安卓实训第三天---简单的用户登录界面,用HTTPURLConnection实现与服务器的连接
- Http 中Connection: close和Connection: keep-alive有什么区别?