Springboot封装HTTPClient中get,post,json,put请求方法
pom.xml依赖如下
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path -->
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
封装get,post请求,贴代码
public class HttpRequestUtil {
public static String getRequest(String url, Map<String,String> params){
String res = "";
boolean flag = true;
Set<String> keys = params.keySet();
for (String key : keys) {
if (flag){
url += "?" + key + "=" + params.get(key);
flag = false;
}else {
url += "&" + key + "=" + params.get(key);
}
}
HttpGet httpGet = new HttpGet(url);
HttpClient httpClient = HttpClients.createDefault();
try {
HttpResponse response = httpClient.execute(httpGet);
res = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
((CloseableHttpClient) httpClient).close();
} catch (IOException e) {
e.printStackTrace();
}
}
return res;
}
public static String postRequest(String url, Map<String,String> params){
String res = "";
HttpPost httpPost = new HttpPost(url);
ArrayList<BasicNameValuePair> basicNameValuePairs = new ArrayList<BasicNameValuePair>();
// 遍历map,放到basicNameValuePairs中
Set<String> keys = params.keySet();
for (String key : keys) {
basicNameValuePairs.add(new BasicNameValuePair(key,params.get(key)));
}
HttpClient httpClient = HttpClients.createDefault();
try {
httpPost.setEntity(new UrlEncodedFormEntity(basicNameValuePairs,"utf-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
res = EntityUtils.toString(httpResponse.getEntity());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
((CloseableHttpClient) httpClient).close();
} catch (IOException e) {
e.printStackTrace();
}
}
return res;
}
public static String sendRequest(String url, String requestType, Map<String, String> parameters){
String response = "";
if ("get".equalsIgnoreCase(requestType)){
response = getRequest(url, parameters);
}else if ("post".equalsIgnoreCase(requestType)){
response = postRequest(url, parameters);
}else {
response = "error request type!!!";
}
return response;
}
}
封装json,put请求,贴代码
public class HttpRequestJsonUtil {
// 声明为静态方法
public static String postRequest(String url, JSONObject jsonObject, JSONObject headers){
String res = "";
HttpPost httpPost = new HttpPost(url);
// 通过形参设置请求头
Set<String> headerkeys = headers.keySet();
for (String headerkey : headerkeys) {
httpPost.addHeader(headerkey.trim(),headers.getString(headerkey).trim());
}
// 发送 json 类型数据
httpPost.setEntity(new StringEntity(jsonObject.toString(),"UTF-8"));
CloseableHttpClient httpClient = HttpClients.createDefault();
// 发送请求
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpPost);
res = EntityUtils.toString(httpResponse.getEntity());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return res;
}
public static String sendRequest(String url, String requestType, JSONObject jsonObject, JSONObject headers){
String response = "";
if ("post".equalsIgnoreCase(requestType)){
response = postRequest(url, jsonObject, headers);
}else {
response = "error request type!!!";
}
return response;
}
}
下一章会出很详细的!
还没有评论,来说两句吧...