/** post
* 调用httpClient
* @param url 要访问的地址
* @param params 要携带的参数
* @return
*/
private String doHttpClient(String url, NameValuePair[] params) {
PostMethod post = new PostMethod(url);
post.setRequestBody(params);
HttpClient client = new HttpClient();
String str_result = ""; // 页面返回结果
try {
client.executeMethod(post); // 执行post方法
str_result = post.getResponseBodyAsString();
return str_result;
} catch (Exception e) {
e.printStackTrace();
} finally {
post.releaseConnection();
}
return "";
}
/**
* 执行一个HTTP GET请求,返回请求响应的HTML
*
* @param url请求的URL地址
* @param queryString 请求的查询参数,可以为null
* @return 返回请求响应的HTML
*/
public static String doGetHttpClient(String url, String queryString) {
String response = null;
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
try {
if (StringUtils.isNotBlank(queryString))
method.setQueryString(URIUtil.encodeQuery(queryString));
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
response = method.getResponseBodyAsString();
}
} catch (URIException e) {
log.error("执行HTTP Get请求时,编码查询字符串“" + queryString + "”发生异常!", e);
} catch (IOException e) {
log.error("执行HTTP Get请求" + url + "时,发生异常!", e);
} finally {
method.releaseConnection();
}
return response;
}