/**
* get 请求
* @return
*/
public static String doHttpGet(String url, List<NameValuePair> params){
String result = null;
//1.获取httpclient
CloseableHttpClient httpClient = HttpClients.createDefault();
//接口返回结果
CloseableHttpResponse response = null;
String paramStr = null;
try {
paramStr = EntityUtils.toString(new UrlEncodedFormEntity(params));
//拼接参数
StringBuffer sb = new StringBuffer();
sb.append(url);
sb.append("?");
sb.append(paramStr);
//2.创建get请求
HttpGet httpGet = new HttpGet(sb.toString());
//3.设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
httpGet.setConfig(requestConfig);
/*此处可以添加一些请求头信息,例如:
httpGet.addHeader("content-type","text/xml");*/
//4.提交参数
response = httpClient.execute(httpGet);
//5.得到响应信息
int statusCode = response.getStatusLine().getStatusCode();
//6.判断响应信息是否正确
if(HttpStatus.SC_OK != statusCode){
//终止并抛出异常
httpGet.abort();
throw new RuntimeException("HttpClientPost,error status code :" + statusCode);
}
//7.转换成实体类
HttpEntity entity = response.getEntity();
if(null != entity){
result = EntityUtils.toString(entity);
}
EntityUtils.consume(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//8.关闭所有资源连接
if(null != response){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != httpClient){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* http post 请求
*/
public static String doPost(String url, String params,String token){
String result = null;
//1. 获取httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
//2. 创建post请求
HttpPost httpPost = new HttpPost(url);
//3.设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
httpPost.setConfig(requestConfig);
httpPost.addHeader("Authorization",token);
//4.提交参数发送请求
StringEntity stringEntity=new StringEntity(params);
// UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params);
// //此处可以设置传输时的编码格式,和数据格式
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
response = httpClient.execute(httpPost);
//5.得到响应信息
int statusCode = response.getStatusLine().getStatusCode();
//6. 判断响应信息是否正确
if(HttpStatus.SC_OK != statusCode){
//结束请求并抛出异常
httpPost.abort();
throw new RuntimeException("HttpClientPost,error status code :" + statusCode);
}
//7. 转换成实体类
HttpEntity entity = response.getEntity();
if(null != 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 {
//8. 关闭所有资源连接
if(null != response){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != httpClient){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
|