Java HttpClient:如何只与服务器建立一个连接?
这段代码为每个请求创建到RESTful服务器的新连接,而不仅仅是使用现有连接。如何更改代码,以便只有一个连接 行响应=oClientCloseable.execute。。。这不仅是任务,而且创建了一个连接 我检查了服务器守护程序日志,唯一的活动是从.execute方法生成的Java HttpClient:如何只与服务器建立一个连接?,java,connection,httpclient,httpresponse,Java,Connection,Httpclient,Httpresponse,这段代码为每个请求创建到RESTful服务器的新连接,而不仅仅是使用现有连接。如何更改代码,以便只有一个连接 行响应=oClientCloseable.execute。。。这不仅是任务,而且创建了一个连接 我检查了服务器守护程序日志,唯一的活动是从.execute方法生成的 import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.ht
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
...
String pathPost = "http://someurl";
String pathDelete = "http://someurl2";
String xmlPost = "myxml";
HttpResponse response = null;
BufferedReader rd = null;
String line = null;
CloseableHttpClient oClientCloseable = HttpClientBuilder.create().setDefaultRequestConfig(defaultRequestConfig).build();
for (int iLoop = 0; iLoop < 25; iLoop++)
{
HttpPost hPost = new HttpPost(pathPost);
hPost.setHeader("Content-Type", "application/xml");
StringEntity se = new StringEntity(xmlPost);
hPost.setEntity(se);
line = "";
try
{
response = oClientCloseable.execute(hPost);
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
while ((line = rd.readLine()) != null)
{
System.out.println(line);
}
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (ConnectionPoolTimeoutException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
HttpClientUtils.closeQuietly(response);
}
HttpDelete hDelete = new HttpDelete(pathDelete);
hDelete.setHeader("Content-Type", "application/xml");
try
{
response = oClientCloseable.execute(hDelete);
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
HttpClientUtils.closeQuietly(response);
}
}
oClientCloseable.close();
这似乎是最相关的,因为它谈论的是消费关闭的连接,连接在响应中。那篇文章也过时了,因为ConsumerContent已被弃用。似乎response.close是正确的方式,但是它关闭了连接,新的响应创建了新的连接
似乎我需要以某种方式创建一个对serer守护进程的响应,然后更改操作get、post、put或delete
关于代码应该如何更改的想法
以下是我使用的一些其他链接:
我执行了Robert Rowntree的建议抱歉,我不确定是否正确引用名称,而是将开头代码替换为:
// Increase max total connection to 200 and increase default max connection per route to 20.
// Configure total max or per route limits for persistent connections
// that can be kept in the pool or leased by the connection manager.
PoolingHttpClientConnectionManager oConnectionMgr = new PoolingHttpClientConnectionManager();
oConnectionMgr.setMaxTotal(200);
oConnectionMgr.setDefaultMaxPerRoute(20);
oConnectionMgr.setMaxPerRoute(new HttpRoute(new HttpHost("192.168.20.120", 8080)), 20);
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setStaleConnectionCheckEnabled(true)
.build();
//HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(defaultRequestConfig).build();
CloseableHttpClient oClientCloseable = HttpClientBuilder.create()
.setConnectionManager(oConnectionMgr)
.setDefaultRequestConfig(defaultRequestConfig)
.build();
我还看到了一堆认证书
我联系了供应商,并使用修改后的版本与他们共享了日志,我的代码是干净的
我的测试示例创建了一个到远程服务器的连接,然后删除该连接并重复多次。每次连接创建请求到达时,他们的代码都会转储身份验证消息
有人指出,从技术上讲,我已经知道,创建到服务的新RESTful连接的线路始终是XXXXX连接允许的。其中有一个,如果你算上我的话,我会在之后进入基于浏览器的界面,以确保我所有的链接都消失了
遗憾的是,我不确定我是否可以使用Apache客户机,所以很遗憾。Apache不支持GET请求中的消息体。对于头脑简单的我来说,在这种情况下,Apache不允许:
GET http://www.example.com/whatevermethod:myport?arg1=data1&arg2=data2
Apache HttpClient->HttpGet没有setEntities命令。研究表明,这是一个POST请求,但服务是它现在的样子,不会改变,因此…您完全可以在Apache HttpClient中使用查询参数:
URIBuilder builder = new URIBuilder("http://www.example.com/whatevermehtod");
builder.addParameter("arg1", "data1");
URI uri = builder.build();
HttpGet get = new HttpGet(uri);
似乎对我起了作用。特别是这个链接:请参见样本中的“总最大或每路线限制”。。。