org.apache.http.client.HttpClient; HttpClient 4.3超时设置
可用的code
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public static String doPost(String url, String params, String contentType) /*throws IOException */{
CloseableHttpClient client = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(httpSoTimeout).setConnectTimeout(connectionTimeout).build();//设置请求和传输超时时间 String strResult = null;
try {
HttpPost post = new HttpPost(url);
post.setConfig(requestConfig);
if (StringUtils.isNotBlank(contentType)) {
post.setHeader("Content-Type", contentType);
}
StringEntity se = new StringEntity(params);
post.setEntity(se); HttpResponse response = client.execute(post); if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK && response.getEntity() != null){
HttpEntity entity = response.getEntity();
strResult = EntityUtils.toString(entity, "utf-8");
EntityUtils.consume(entity);
}
} catch (Exception e) {
log.error("http post error:{} ", e.getMessage());
return null;
}
return strResult;
}
}
最近用到了HttpClient写爬虫,可能我有新版本强迫症,老是喜欢用新版本的东西(虽说新版本不一定好用),然后就用了HttpClient 4.3。HttpClient这货和Lucene一样,每个版本的API都变化很大,这有点让人头疼。就好比创建一个HttpClient对象吧,每一个版本的都不一样,
3.X是这样的
HttpClient httpClient=new DefaultHttpClient();
4.3是这样的
CloseableHttpClient httpClient = HttpClients.createDefault();
当然,上面这些变化只不过是一些小变化,大家看看API大家就都会了。
我要讲的是超时设置,HttpClient有三种超时设置,最近比较忙,没时间具体归纳总结,以后再补上,我这里就讲一些最简单最易用的超时设置方法。
这是个3.X的超时设置方法
HttpClient client = new HttpClient();
client.setConnectionTimeout(30000);
client.setTimeout(30000);
HttpClient httpClient= new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
4.X版本的超时设置(4.3后已过时)
HttpClient httpClient=new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,2000);//连接时间
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,2000);//数据传输时间
4.3版本超时设置
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet=new HttpGet("http://www.baidu.com");//HTTP Get请求(POST雷同)
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();//设置请求和传输超时时间
httpGet.setConfig(requestConfig);
httpClient.execute(httpGet);//执行请求
BTW,4.3版本不设置超时的话,一旦服务器没有响应,等待时间N久(>24小时)。
org.apache.http.client.HttpClient; HttpClient 4.3超时设置的更多相关文章
- Java中httpClient中三种超时设置
本文章给大家介绍一下关于Java中httpClient中的三种超时设置小结 在Apache的HttpClient包中,有三个设置超时的地方: /* 从连接池中取连接的超时时间*/ ConnManage ...
- apache.http.client.HttpClient
前言 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 java net包中已经提 ...
- org.apache.commons.httpclient和org.apache.http.client区别(转)
官网说明: http://hc.apache.org/httpclient-3.x/ Commons HttpClient项目现已结束,不再开发.它已被其HttpClient和HttpCore模块中的 ...
- org.apache.http.client.HttpClient使用方法
一.org.apache.commons.httpclient和org.apache.http.client区别(转) 官网说明: http://hc.apache.org/httpclient- ...
- Java之网络请求工具类(依赖:org.apache.http;注:HttpClient 4.4,HttpCore 4.4)
到此处可以去下载依赖包:http://hc.apache.org/downloads.cgi import java.util.List; import org.apache.http.HttpSta ...
- org.apache.commons.httpclient.HttpClient的使用(转)
HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 java net包中已经提供了访 ...
- org.apache.http.client.CircularRedirectException: Circular redirect to "http://xxx"问题解决
org.apache.http.client.CircularRedirectException: Circular redirect to "http://xxx"问题解决 ...
- httpclient发送request请求时设置header和timeout
package com.xxx.xxx.common; import java.io.BufferedReader; import java.io.InputStreamReader; import ...
- Apache HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页
Apache HttpComponents Client 4.0已经发布多时,httpclient项目从commons子项目挪到了HttpComponents子项目下,httpclient3.1和 h ...
随机推荐
- nutch1.4 在windows下面提示 java.io.IOException: CreateProcess error=2, ϵͳÕҲ»µ½ָ¶
eclipse运行nutch1.4在window下面提示异常解决 需要安装cynwin,被设置环境变量 1:安装cygwin 注:在选择要安装的软件包的时候我选择了在All这一行上后面的Default ...
- highstock-处理时间需要处理世界时间偏移量
highstock的数据格式采用的是[[时间,数据],[时间,数据],[时间,数据],[时间,数据]],而时间采用的是13位的毫秒值,如[1133136000000,69.66],采用的时间格式为UT ...
- request 和response
当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, JSP,Perl, AJAX 等等. 无论Web技术在未来如何发展,理解Web程序之间通信的基本协议相当重要, 因为它让我们理解了We ...
- 【POJ 3525】Most Distant Point from the Sea(直线平移、半平面交)
按逆时针顺序给出n个点,求它们组成的多边形的最大内切圆半径. 二分这个半径,将所有直线向多边形中心平移r距离,如果半平面交不存在那么r大了,否则r小了. 平移直线就是对于向量ab,因为是逆时针的,向中 ...
- highchart去掉highcharts.com及导出
右下角默认会有highcharts.com credits: { enabled:false} 导出服务用于Highcharts导出功能,即通过导出服务器将图表导出为常见图片格式或 PDF 文档. 默 ...
- C#制作验证码
void CodeImage(string code) { if (code == null || code.Trim() == string.Empty) return; System.Drawin ...
- Windows装机必备软件列表
经常装系统,列个List,以后装完之后安装软件直接参照使用!windows版: 输入法: 搜狗输入法(由于长期使用导致此输入法十分熟悉我的输入习惯,以无法自拔).支持Linux.Windows(太穷还 ...
- Objective-C Runtime与黑客帝国
Runtime的消息机制让我容易想起黑客帝国的Matrix.而OC语言,就像是架构在C语言真实世界上的Matrix世界,Runtime接管了这个虚拟世界到真实世界的承接. 在黑客帝国里,Matrix的 ...
- C++ 之const Member Functions
Extraction from C++ primer 5th Edition 7.1.2 The purpose of the const that follows the parameter lis ...
- JavaScript 写几个简单的知识点
首先,还是用比较官方的文字描述来解释下JavaScript: JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为 ...