项目中会遇到需要发送请求获取不同服务器中的资源,此时不能使用转发或者重定向,而使用httpclient可以实现。下面介绍httpclient中的get请求和post请求:
GET方法:
public static String doGet() {
String result = null;
//请求地址
String url = "";
//获取请求参数
List<NameValuePair> parame = new ArrayList<NameValuePair>();
parame.add(new BasicNameValuePair("参数名", 参数值);
// 获取httpclient
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String parameStr = null;
try {
parameStr = EntityUtils.toString(new UrlEncodedFormEntity(parame));
//拼接参数
StringBuffer sb = new StringBuffer();
sb.append(url);
sb.append("?");
sb.append(parameStr);
//创建get请求
HttpGet httpGet = new HttpGet(sb.toString());
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(2000).setConnectTimeout(2000).build();
httpGet.setConfig(requestConfig);
// 提交参数发送请求
response = httpclient.execute(httpGet);
// 得到响应信息
int statusCode = response.getStatusLine().getStatusCode();
// 判断响应信息是否正确
if (statusCode != HttpStatus.SC_OK) {
// 终止并抛出异常
httpGet.abort();
throw new RuntimeException("HttpClient,error status code :" + statusCode);
}
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
EntityUtils.consume(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭所有资源连接
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result.toString();
}
POST方法:
public static String doPost(){
String result = null;
//请求地址
String url = "";
List<NameValuePair> parameForToken = new ArrayList<NameValuePair>();
parameForToken.add("参数名", 参数值);
// 获取httpclient
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
//创建post请求
HttpPost httpPost = new HttpPost(url);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(2000).setConnectTimeout(2000).build();
httpPost.setConfig(requestConfig);
// 提交参数发送请求
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(parame);
httpPost.setEntity(urlEncodedFormEntity);
response = httpclient.execute(httpPost);
// 得到响应信息
int statusCode = response.getStatusLine().getStatusCode();
// 判断响应信息是否正确
if (statusCode != HttpStatus.SC_OK) {
// 终止并抛出异常
httpPost.abort();
throw new RuntimeException("HttpClient,error status code :" + statusCode);
}
HttpEntity entity = response.getEntity();
if (entity != null) {
//result = EntityUtils.toString(entity);//不进行编码设置
result = EntityUtils.toString(entity, "UTF-8");
}
EntityUtils.consume(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭所有资源连接
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
方法中主要用到的jar包: