HttpClient的使用
HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。
http的主要功能包括:
1. 实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)
2. 支持自动转向
3. 支持 HTTPS 协议
4. 支持代理服务器等
使用 HttpClient 需要以下 6 个步骤:
1. 创建 HttpClient 的实例
2. 创建某种连接方法的实例,在这里是 GetMethod。在 GetMethod 的构造函数中传入待连接的地址
3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
4. 读 response
5. 释放连接。无论执行方法是否成功,都必须释放连接
6. 对得到后的内容进行处理
根据以上步骤,我们来编写用GET方法来取得某网页内容的代码。
大部分情况下 HttpClient 默认的构造函数已经足够使用。HttpClient httpClient = new HttpClient(); |
GetMethod getMethod = new GetMethod(http://www.ibm.com/); |
//设置成了默认的恢复策略,在发生异常时候将自动重试3次,在这里你也可以设置成自定义的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
//执行getMethod
int statusCode = client.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + getMethod.getStatusLine());
}
//设置成了默认的恢复策略,在发生异常时候将自动重试3次,在这里你也可以设置成自定义的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
//执行getMethod
int statusCode = client.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + getMethod.getStatusLine());
}在返回的状态码正确后,即可取得内容。取得目标地址的内容有三种方法:第一种,getResponseBody,该方法返回的是目标的二进制的byte流;第二种,getResponseBodyAsString,这个方法返回的是String类型,值得注意的是该方法返回的String的编码是根据系统默认的编码方式,所以返回的String值可能编码类型有误,在本文的"字符编码"部分中将对此做详细介绍;第三种,getResponseBodyAsStream,这个方法对于目标地址中有大量数据需要传输是最佳的。在这里我们使用了最简单的getResponseBody方法。 释放连接。无论执行方法是否成功,都必须释放连接。 method.releaseConnection(); |
System.out.println(new String(responseBody)); |
完整的代码示例
public class FeedBackServiceImpl implements FeedBackService, InitializingBean {
private static final Logger log = LoggerFactory.getLogger(FeedBackServiceImpl.class);
private static final int CONNECTION_TIME_OUT = 1000;
private static final int TIME_OUT = 2000;
/**
* 参数
*/
private static final String PARAM_ID = "id";
private static final String PARAM_QUESTION_ID = "questionId";
private static final String PARAM_ANSWER = "answer";
private static final String PARAM_COOKIE_ID = "cookieId";
/**
* 盖娅系统获取调查问卷的url
*/
private String feedBackQuestionaireUrl;
/**
* 盖娅系统保存用户反馈信息的url
*/
private String feedBackAnswerUrl;
/**
* 问卷类型与问卷id的映射
*
* <pre>
* 问卷类型,从前台传递过来。目前有两个取值:"1"表示进货单页面的问卷;"2"表示确认订单页面的问卷
* 问卷id,作为从盖娅系统中获取调查问卷的参数
* </pre>
*/
private Map<String, String> questionTypeToQuestionId = new HashMap<String, String>();
private HttpClient httpClient;
public boolean addFeedBack(FeedBackModel feedBackModel) {
if (feedBackModel == null) {
return false;
}
boolean res = false;
// 调用盖娅系统的接口,提交反馈
PostMethod postMethod = new PostMethod(feedBackAnswerUrl);
postMethod.addParameter(PARAM_QUESTION_ID, feedBackModel.getQuestionId());
postMethod.addParameter(PARAM_ANSWER, feedBackModel.getAnswer());
postMethod.addParameter(PARAM_COOKIE_ID, feedBackModel.getCookieId());
try {
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK) {
StringBuilder sb = new StringBuilder("fail to add feedback, requestUrl=");
sb.append(feedBackAnswerUrl).append(", parameter: ").append(feedBackModel.toString()).append(", HTTP StatusCode=").append(statusCode);
log.error(sb.toString());
res = false;
} else {
res = true;
}
} catch (HttpException e) {
log.error("HttpException occured when addFeedBack, requestUrl=" + feedBackAnswerUrl + ", parameter: " + feedBackModel.toString(), e);
res = false;
} catch (IOException e) {
log.error("IOException occured when addFeedBack, requestUrl=" + feedBackAnswerUrl + ", parameter: " + feedBackModel.toString(), e);
res = false;
} finally {
postMethod.releaseConnection();
postMethod = null;
}
return res;
}
public JSONObject getQuestionaire(String questionType) {
String id = questionTypeToQuestionId.get(questionType);
if (StringUtil.isBlank(id)) {
return null;
}
// 调用盖娅系统的接口,获取调查问卷
GetMethod getMethod = new GetMethod(feedBackQuestionaireUrl);
NameValuePair param = new NameValuePair(PARAM_ID, id);
getMethod.setQueryString(new NameValuePair[]{param});
String responseText = null;
try {
// 执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
StringBuilder sb = new StringBuilder("fail to getQuestionaire, requestUrl=");
sb.append(feedBackQuestionaireUrl).append("?id=").append(id).append(", HTTP StatusCode=").append(statusCode);
log.error(sb.toString());
return null;
}
// 读取内容
responseText = getMethod.getResponseBodyAsString();
} catch (HttpException e) {
StringBuilder sb = new StringBuilder("HttpException occured when getQuestionaire, requestUrl=");
sb.append(feedBackQuestionaireUrl).append("?id=").append(id);
log.error(sb.toString(), e);
return null;
} catch (IOException e) {
StringBuilder sb = new StringBuilder("IOException occured when getQuestionaire, requestUrl=");
sb.append(feedBackQuestionaireUrl).append("?id=").append(id);
log.error(sb.toString(), e);
return null;
} finally {
getMethod.releaseConnection();
getMethod = null;
}
if (StringUtil.isBlank(responseText)) {
return null;
}
// 从查询结果中解析出包含问卷的json字符串
int index = responseText.indexOf("=");
if (index >= 0) {
responseText = responseText.substring(index + 1);
}
try {
JSONObject json = JSONObject.fromObject(responseText);
return json;
} catch (Exception e) {
log.error("fail to change from String to JSONObject, string=" + responseText, e);
return null;
}
}
public void afterPropertiesSet() throws Exception {
HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
httpClient = new HttpClient(connectionManager);
httpClient.setConnectionTimeout(CONNECTION_TIME_OUT);
httpClient.setTimeout(TIME_OUT);
} 相关推荐
84487600 2020-08-16
似水流年梦 2020-08-09
knightwatch 2020-07-26
fengchao000 2020-06-16
标题无所谓 2020-06-14
sicceer 2020-06-12
yanghui0 2020-06-09
yanghui0 2020-06-09
创建一个 HttpClient 实例,这个实例需要调用 Dispose 方法释放资源,这里使用了 using 语句。接着调用 GetAsync,给它传递要调用的方法的地址,向服务器发送 Get 请求。
wanghongsha 2020-06-04
jiaguoquan00 2020-05-26
zhaolisha 2020-05-16
wanghongsha 2020-05-05
wanghongsha 2020-04-14
knightwatch 2020-04-11
hygbuaa 2020-03-27
zergxixi 2020-03-24