java httpclient 1.4,java – 如何保持HttpClient连接保持活跃?

我正在使用HttpClient POST方法.我需要创建一次HttpClient,并且应该使用Keep Alive Connection.但我想在我的情况下,它每次都建立一个新的连接.

因此,我需要为HttpClient使用Keep Alive连接.

这是我的代码片段任何帮助将不胜感激.

ClientConnectionManager mgr = httpclient_recv.getConnectionManager();

hp = httpclient_recv.getParams();

httpclient_recv = new DefaultHttpClient(

new ThreadSafeClientConnManager(hp,mgr.getSchemeRegistry()), hp);

while (true) {

try {

java.util.logging.Logger.getLogger("org.apache.http.wire")

.setLevel(java.util.logging.Level.FINER);

java.util.logging.Logger.getLogger("org.apache.http.headers")

.setLevel(java.util.logging.Level.FINER);

System.setProperty("org.apache.commons.logging.Log",

"org.apache.commons.logging.impl.SimpleLog");

System.setProperty(

"org.apache.commons.logging.simplelog.showdatetime",

"true");

System.setProperty(

"org.apache.commons.logging.simplelog.log.httpclient.wire",

"debug");

System.setProperty(

"org.apache.commons.logging.simplelog.log.org.apache.http",

"debug");

System.setProperty(

"org.apache.commons.logging.simplelog.log.org.apache.http.headers",

"debug");

ByteArrayEntity bae = new ByteArrayEntity(byteData);

bae.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,

"binary/octet-stream"));

httppost_recv.setHeader(HTTP.CONN_DIRECTIVE,HTTP.CONN_KEEP_ALIVE);

httppost_recv.setEntity(bae);

{

System.out.println("res b4 response");

response_recv = httpclient_recv

.execute(httppost_recv);

response_recv.getEntity().consumeContent();

System.out.println("res a4 response");

if (response_recv != null) {

byteArray = EntityUtils.toByteArray(response_recv

.getEntity());

playing = true;

}

}

}

而且logcat日志是:

12-03 10:07:29.466: I/System.out(1529): res b4 response

12-03 10:07:29.646: D/org.apache.http.wire(1529): >> "POST /ping HTTP/1.1[EOL]"

12-03 10:07:29.666: D/org.apache.http.wire(1529): >> "Connection: Keep-Alive[EOL]"

12-03 10:07:29.686: D/org.apache.http.wire(1529): >> "Content-Length: 1[EOL]"

12-03 10:07:29.705: D/org.apache.http.wire(1529): >> "Content-Type: binary/octet-stream[EOL]"

12-03 10:07:29.716: D/org.apache.http.wire(1529): >> "Host: 192.168.1.36[EOL]"

12-03 10:07:29.725: D/org.apache.http.wire(1529): >> "User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)[EOL]"

12-03 10:07:29.736: D/org.apache.http.wire(1529): >> "[EOL]"

12-03 10:07:29.746: D/org.apache.http.headers(1529): >> POST /ping HTTP/1.1

12-03 10:07:29.746: D/org.apache.http.headers(1529): >> Connection: Keep-Alive

12-03 10:07:29.756: D/org.apache.http.headers(1529): >> Content-Length: 1

12-03 10:07:29.756: D/org.apache.http.headers(1529): >> Content-Type: binary/octet-stream

12-03 10:07:29.765: D/org.apache.http.headers(1529): >> Host: 192.168.1.36

12-03 10:07:29.765: D/org.apache.http.headers(1529): >> User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)

12-03 10:07:29.776: D/org.apache.http.wire(1529): >> "[0x0]"

12-03 10:07:29.796: D/org.apache.http.wire(1529): << "HTTP/1.1 200 OK[EOL]"

12-03 10:07:29.805: D/org.apache.http.wire(1529): << "Server: gSOAP/2.8[EOL]"

12-03 10:07:29.816: D/org.apache.http.wire(1529): << "Content-Type: binary/octet-stream[EOL]"

12-03 10:07:29.826: D/org.apache.http.wire(1529): << "Content-Length: 2048[EOL]"

12-03 10:07:29.836: D/org.apache.http.wire(1529): << **"Connection: close[EOL]"**

12-03 10:07:29.887: D/org.apache.http.headers(1529): << HTTP/1.1 200 OK

12-03 10:07:29.896: D/org.apache.http.headers(1529): << Server: gSOAP/2.8

12-03 10:07:29.896: D/org.apache.http.headers(1529): << Content-Type: binary/octet-stream

12-03 10:07:29.906: D/org.apache.http.headers(1529): << Content-Length: 2048

12-03 10:07:29.906: D/org.apache.http.headers(1529): << **Connection: close**

12-03 10:07:29.916: I/System.out(1529): res a4 response

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值