加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 服务器 > 安全 > 正文

HTTP长连接与短连接使用方法及测试详解

发布时间:2020-05-11 11:16:04 所属栏目:安全 来源:站长网
导读:副标题#e# HTTP短连接(非持久连接)是指,客户端和服务端进行一次HTTP请求/响应之后,就关闭连接。所以,下一次的HTTP请求/响应操作就需要重新建立连接。 HTTP长连接(持久连接)是指,客户端和服务端建立一次连接之后,可以在这条连接上进行多次请求/响应

//org.apache.http.impl.execchain.MainClientExec#execute ...... //从连接池中lease connection final HttpClientConnectionmanagedConn = connRequest.get(timeout > 0 ? timeout : 0, TimeUnit.MILLISECONDS); ...... //将conenction封装在ConnectionHolder中 final ConnectionHolder connHolder = new ConnectionHolder(this.log, this.connManager, managedConn); ...... // The connection is in or can be brought to a re-usable state. //如果返回值消息头中connection设置为close,则返回false if (reuseStrategy.keepAlive(response, context)) { // Set the idle duration of this connection //取出response消息头中,keep-alive的timeout值 final long duration = keepAliveStrategy.getKeepAliveDuration(response, context); if (this.log.isDebugEnabled()) { final String s; if (duration > 0) { s = "for " + duration + " " + TimeUnit.MILLISECONDS; } else { s = "indefinitely"; } this.log.debug("Connection can be kept alive " + s); } //设置失效时间 connHolder.setValidFor(duration, TimeUnit.MILLISECONDS); connHolder.markReusable(); } else { connHolder.markNonReusable(); }

待读取响应之后,释放连接,即:connHolder.releaseConnection()。调用org.apache.http.impl.conn.PoolingHttpClientConnectionManager#releaseConnection方法。

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读