Java HTTP客户端请求集超时

Java HTTP客户端请求集超时,java,Java,我想设置请求的超时时间。这是代码,到目前为止我已经知道了 final HttpClient httpclient = HttpClients.createDefault(); final HttpPost httppost = new HttpPost(address); httppost.setHeader("Accept", "text/xml"); httppost.setHeader("Content-type", "application/xml; charset=UTF-8"); h

我想设置请求的超时时间。这是代码,到目前为止我已经知道了

final HttpClient httpclient = HttpClients.createDefault();
final HttpPost httppost = new HttpPost(address);
httppost.setHeader("Accept", "text/xml");
httppost.setHeader("Content-type", "application/xml; charset=UTF-8");
httppost.setEntity(new StringEntity(body));
final HttpResponse response = httpclient.execute(httppost);
final HttpEntity entity = response.getEntity();
我尝试过(不起作用,继续加载并忽略超时)

并且(这个抛出
java.lang.UnsupportedOperationException


有没有其他方法来设定时间?我真的不需要响应,像异步请求这样的东西也可以完成这项工作。

Apache的
HttpClient
有两个独立的超时:一个超时用于等待多长时间来建立TCP连接,另一个超时用于等待多长时间来等待后续的数据字节

用于建立TCP连接,而在等待后续数据字节时使用

// Creating default HttpClient
HttpClient httpClient = new DefaultHttpClient();
final HttpParams httpParams = httpClient.getParams();

// Setting timeouts
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 30000);

// Rest of your code
final HttpPost httppost = new HttpPost(address);
httppost.setHeader("Accept", "text/xml");
httppost.setHeader("Content-type", "application/xml; charset=UTF-8");
httppost.setEntity(new StringEntity(body));
final HttpResponse response = httpclient.execute(httppost);
final HttpEntity entity = response.getEntity();

Apache的
HttpClient
有两个独立的超时:一个超时用于等待多长时间建立TCP连接,另一个超时用于等待后续数据字节

用于建立TCP连接,而在等待后续数据字节时使用

// Creating default HttpClient
HttpClient httpClient = new DefaultHttpClient();
final HttpParams httpParams = httpClient.getParams();

// Setting timeouts
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 30000);

// Rest of your code
final HttpPost httppost = new HttpPost(address);
httppost.setHeader("Accept", "text/xml");
httppost.setHeader("Content-type", "application/xml; charset=UTF-8");
httppost.setEntity(new StringEntity(body));
final HttpResponse response = httpclient.execute(httppost);
final HttpEntity entity = response.getEntity();

这不是错了吗
final-HttpClient-HttpClient;=HttpClients.createDefault()你有额外的;当你说“不工作”时,你看到了什么行为?@JqueryLearner不,这只是一个复制粘贴错误。@christopher nothing它一直在加载和阻塞Java线程。在我的程序中,我将其设置为3秒,10秒后仍会加载。您尝试了
CoreConnectionPNames。超时时间
?这不是错误的
final HttpClient HttpClient;=HttpClients.createDefault()你有额外的;当你说“不工作”时,你看到了什么行为?@JqueryLearner不,这只是一个复制粘贴错误。@christopher nothing它一直在加载和阻塞Java线程。在我的程序中,我将其设置为3秒,10秒后仍会加载。您尝试了
CoreConnectionPNames.TIMEOUT
// Creating default HttpClient
HttpClient httpClient = new DefaultHttpClient();
final HttpParams httpParams = httpClient.getParams();

// Setting timeouts
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 30000);

// Rest of your code
final HttpPost httppost = new HttpPost(address);
httppost.setHeader("Accept", "text/xml");
httppost.setHeader("Content-type", "application/xml; charset=UTF-8");
httppost.setEntity(new StringEntity(body));
final HttpResponse response = httpclient.execute(httppost);
final HttpEntity entity = response.getEntity();