该工具类使用到的jar包
//使用maven管理jar包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.40</version>
</dependency>
该工具类中涉及到的方法
get无参
/**
* get无参数请求 ,可验收
*
* @param url
* @return resultMap ---> 状态码:statusLine,响应内容长度:contentLength,响应内容:content
*/
public static Map<String, Object> doGet(String url) {
//返回参数
Map<String, Object> resultMap = new HashMap<String, Object>();
//获得一个http客户端
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//创建一个get请求
HttpGet httpGet = new HttpGet(url);
//请求响应模型
CloseableHttpResponse httpResponse = null;
try {
//客户端发送get请求,并获得具体响应
httpResponse = httpClient.execute(httpGet);
//从响应模型中获取实体
HttpEntity httpEntity = httpResponse.getEntity();
//从响应模型中获取响应状态码
StatusLine statusLine = httpResponse.getStatusLine();
resultMap.put("statusLine", statusLine);
if (null != httpResponse) {
//响应内容长度
long contentLength = httpEntity.getContentLength();
//响应内容
String content = EntityUtils.toString(httpEntity);
resultMap.put("contentLength", contentLength);
resultMap.put("content", content);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//资源释放
if (null != httpResponse) {
httpResponse.close();
}
if (null != httpClient) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
return resultMap;
}
}
}
get有参
/**
* get有参请求 ,可验收
*
* @param url 请求路径
* @param params 请求参数,拼接完成 如:name=张三&age=20
* @return resultMap ---> 状态码:statusLine,响应内容长度:contentLength,响应内容:content
*/
public static Map<String, Object> doGet(String url, String params) {
//返回参数
Map<String, Object> resultMap = new HashMap<String, Object>();
//获得一个http客户端
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//请求参数,编码转换,避免特殊字符串无法传递.当参数乱码时,可将该段代码放开
/*try {
params = URLEncoder.encode(params, "utf8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}*/
//创建一个get请求
HttpGet httpGet = new HttpGet(url + "?" + params);
System.out.println(url + "?" + params);
//请求响应模型
CloseableHttpResponse httpResponse = null;
//创建配置信息对象
RequestConfig requestConfig = RequestConfig.custom()
//设置连接超时时间
.setConnectTimeout(5000)
//设置请求超时时间
.setConnectionRequestTimeout(5000)
//设置读写超时时间
.setSocketTimeout(5000)
//设置是否允许重定向
.setRedirectsEnabled(true)
.build();
//将配置信息修改入请求中
httpGet.setConfig(requestConfig);
try {
//客户端发送get请求,并获得具体响应
httpResponse = httpClient.execute(httpGet);
//从响应模型中获取实体
HttpEntity httpEntity = httpResponse.getEntity();
//从响应模型中获取响应状态码
StatusLine statusLine = httpResponse.getStatusLine();
resultMap.put("statusLine", statusLine);
if (null != httpResponse) {
//响应内容长度
long contentLength = httpEntity.getContentLength();
//响应内容
String content = EntityUtils.toString(httpEntity);
resultMap.put("contentLength", contentLength);
resultMap.put("content", content);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//资源释放
if (null != httpResponse) {
httpResponse.close();
}
if (null != httpClient) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
return resultMap;
}
}
}
post无参
/**
* post无参请求 , 可验收
* @param url
* @return resultMap ---> 状态码:statusLine,响应内容长度:contentLength,响应内容:content
*/
public static Map<String, Object> doPost(String url) {
//返回参数准备
Map<String, Object> resultMap = new HashMap<String, Object>();
//获得一个http客户端
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//创建一个post请求
HttpPost httpPost = new HttpPost(url);
//创建一个可关闭的响应模型
CloseableHttpResponse httpResponse = null;
try {
//http客户端发送post请求
httpResponse = httpClient.execute(httpPost);
//从响应模型中获取实体
HttpEntity httpEntity = httpResponse.getEntity();
//从响应模型中获取响应状态码
StatusLine statusLine = httpResponse.getStatusLine();
resultMap.put("statusLine", statusLine);
if (null != httpResponse) {
//响应内容长度
long contentLength = httpEntity.getContentLength();
//响应内容
String content = EntityUtils.toString(httpEntity);
resultMap.put("contentLength", contentLength);
resultMap.put("content", content);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//资源释放
if (null != httpResponse) {
httpResponse.close();
}
if (null != httpClient) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
return resultMap;
}
}
}
post有参
/**
* post有参请求 , 可验收
* @param url
* @param object 对象参数
* @return resultMap ---> 状态码:statusLine,响应内容长度:contentLength,响应内容:content
*/
public static Map<String, Object> doPost(String url, Object object) {
//请求参数准备
Map<String,Object> resultMap = new HashMap<String,Object>();
//创建http客户端
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//创建一个post请求
HttpPost httpPost = new HttpPost(url);
//生成object的相应json字符串
String jsonString = JSON.toJSONString(object);
//对字符串进行相应的转码
StringEntity stringEntity = new StringEntity(jsonString,"UTF-8");
//将请求体放置post请求中
httpPost.setEntity(stringEntity);
//设置请求头,json类型
httpPost.setHeader("Content-Type","application/json;charset=utf8");
//创建一个可关闭的响应模型
CloseableHttpResponse httpResponse = null;
try {
//http客户端发送post请求
httpResponse = httpClient.execute(httpPost);
//从响应模型中获取响应体
HttpEntity httpEntity = httpResponse.getEntity();
//从响应模型中获取响应状态码
StatusLine statusLine = httpResponse.getStatusLine();
resultMap.put("statusLine", statusLine);
if (null != httpResponse) {
//响应内容长度
long contentLength = httpEntity.getContentLength();
//响应内容
String content = EntityUtils.toString(httpEntity);
resultMap.put("contentLength", contentLength);
resultMap.put("content", content);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//资源释放
if (null != httpResponse) {
httpResponse.close();
}
if (null != httpClient) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
return resultMap;
}
}
}
个人心得
1、整理的还不是很全面,只涉及到了post传参为对象的时候的方法
2、自己测试后,可以使用,希望自己以后的工作中可以用自己的代码
3、不断的积累,让自己遇到问题不再发愁