标题
包含get和post的有参和无参的请求 post有参请求用httpEntity接受,处理成JsonObject类型
方法
package com.shzu.xyf.finding_sql_install_system.finding_sql_install_system.common;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClientUtils {
public static HttpEntity getWithoutParams(String url) {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
HttpEntity responseEntity = response.getEntity();
System.out.println("响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
return responseEntity;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static HttpEntity getWithParams(String url, Map<String, String> paramMap) {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
StringBuffer params = new StringBuffer();
try {
for (Map.Entry<String, String> paramsEntry : paramMap.entrySet()) {
params.append(paramsEntry.getKey() + "=" + URLEncoder.encode(paramsEntry.getValue(), "utf-8"));
params.append("&");
}
params.deleteCharAt(params.length() - 1);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
HttpGet httpGet = new HttpGet(url + "?" + params);
CloseableHttpResponse response = null;
try {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setSocketTimeout(5000)
.setRedirectsEnabled(true).build();
httpGet.setConfig(requestConfig);
response = httpClient.execute(httpGet);
HttpEntity responseEntity = response.getEntity();
System.out.println("响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
return responseEntity;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static HttpEntity postWithoutParam(String url){
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
System.out.println("响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
return responseEntity;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static String getStrToPostWithParams(String url,JSONObject params) throws URISyntaxException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
URI uri = new URI(url);
HttpPost httpPost = new HttpPost(uri);
StringEntity entity = new StringEntity(JSON.toJSONString(params), "UTF-8");
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
byte[] bytes = EntityUtils.toByteArray(response.getEntity());
System.out.println("响应状态为:" + response.getStatusLine());
if (bytes != null) {
String resultStr = new String(bytes,"UTF-8");
JSONObject jsonObject = JSONObject.parseObject(resultStr);
return jsonObject.toJSONString();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
Demo
public class demo {
public static void main(String[] args) throws JSONException, URISyntaxException, IOException {
String url = "http://localhost:8000/post";
JSONObject params = new JSONObject();
params.put("eee","eee");
String strToPostWithParams = HttpClientUtils.getStrToPostWithParams(url, params);
JSONObject jsonObject = JSONObject.parseObject(strToPostWithParams);
System.out.println(jsonObject.get("code"));
}
}
测试接口
@PostMapping("/post")
@ResponseBody
public JSONObject test(@RequestBody JSONObject data){
System.out.println(data);
JSONObject result = new JSONObject();
result.put("code",1);
return result;
}
|