工具类
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;import java.util.HashMap;
import java.util.Map;
import java.util.Set;public class HttpClientUtil {public static String sendPostWithJson(String url, String jsonStr, HashMap<String,String> headers) {// 返回的结果String jsonResult = "";try {HttpClient client = new HttpClient();// 连接超时client.getHttpConnectionManager().getParams().setConnectionTimeout(3*1000);// 读取数据超时client.getHttpConnectionManager().getParams().setSoTimeout(3*60*1000);client.getParams().setContentCharset("UTF-8");PostMethod postMethod = new PostMethod(url);//循环headersSet<String> keys = headers.keySet();for (String key : keys) {String value = headers.get(key);postMethod.setRequestHeader(key, value);}// 非空if (null != jsonStr && !"".equals(jsonStr)) {StringRequestEntity requestEntity = new StringRequestEntity(jsonStr, headers.get("content-type"), "UTF-8");postMethod.setRequestEntity(requestEntity);}int status = client.executeMethod(postMethod);if (status == HttpStatus.SC_OK) {jsonResult = postMethod.getResponseBodyAsString();} else {throw new RuntimeException("接口连接失败!");}} catch (Exception e) {throw new RuntimeException("接口连接失败!");}return jsonResult;}public static String sendGetWithJson(String url, HashMap<String, String> headers) {// 返回的结果String jsonResult = "";try {HttpClient client = new HttpClient();// 连接超时client.getHttpConnectionManager().getParams().setConnectionTimeout(3 * 1000);// 读取数据超时client.getHttpConnectionManager().getParams().setSoTimeout(3 * 60 * 1000);client.getParams().setContentCharset("UTF-8");GetMethod getMethod = new GetMethod(url);// 设置请求头if (headers != null && !headers.isEmpty()) {for (Map.Entry<String, String> entry : headers.entrySet()) {getMethod.setRequestHeader(entry.getKey(), entry.getValue());}}int status = client.executeMethod(getMethod);if (status == HttpStatus.SC_OK) {jsonResult = getMethod.getResponseBodyAsString();} else {throw new RuntimeException("接口连接失败!状态码:" + status);}} catch (Exception e) {throw new RuntimeException("接口连接失败!", e);}return jsonResult;}
}
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.project.market.service.dto.ExtraDto;
import org.apache.poi.ss.formula.functions.T;import java.util.Map;public class JsonUtil {//string转换成json的方法public static JsonNode stringToJson(String jsonStr) throws JsonProcessingException {ObjectMapper mapper = new ObjectMapper();return mapper.readTree(jsonStr);}//将map转换成string串的jsonpublic static String mapToJson(Map map) throws JsonProcessingException {ObjectMapper objectMapper = new ObjectMapper();try {return objectMapper.writeValueAsString(map);} catch (Exception e) {e.printStackTrace();return "";}}//将JsonNode类型转换成string类型public static String jsonNodeToString(JsonNode jsonNode){ObjectMapper objectMapper = new ObjectMapper();try {return objectMapper.writeValueAsString(jsonNode);} catch (Exception e) {e.printStackTrace();return "";}}//将str类型的json数据转换成对象public static <T> T strToClass(String str , Class<T> t) throws JsonProcessingException {ObjectMapper objectMapper = new ObjectMapper();return objectMapper.readValue(str, t);}
}
get请求
public JsonNode getDemo(String requestUrl) {try {HashMap<String, String> headers = new HashMap<>(3);headers.put("content-type", "application/json");// 发送post请求String resultData = HttpClientUtil.sendGetWithJson(requestUrl,headers);// 并接收返回结果JsonNode jsonNode = JsonUtil.stringToJson(resultData);String code = jsonNode.get("code").toString().replace("\"", "");if ("200".equals(code)) {return jsonNode;}return null;} catch (Exception e) {throw new RuntimeException(e);}}
post请求
public Map<String,Object> map(String mapValue1, String mapValue2, String mapValue3){Map<String,Object> returnMap = new HashMap<>();map.put("mapKey1",mapValue1);map.put("mapKey2", mapValue2);map.put("mapKey3",mapValue3);return returnMap;}public Map<String,String> postDemo(String requestUrl) {try {HashMap<String, String> headers = new HashMap<>(3);String jsonStr = JsonUtil.mapToJson(map(mapValue1, mapValue2, mapValue3));headers.put("header1", "value1");headers.put("header2", "value2");headers.put("content-type", "application/json");// 发送post请求String resultData = HttpClientUtil.sendPostWithJson(requestUrl, jsonStr, headers);// 并接收返回结果JsonNode jsonNode = JsonUtil.stringToJson(resultData);HashMap<String, String> resultMap = new HashMap<>();String errCode = jsonNode.get("errCode").toString().replace("\"", "");if ("200".equals(errCode)) {resultMap.put("key1",jsonNode.path("data").path("key1").asText());resultMap.put("key2",jsonNode.path("data").path("key2").asText());resultMap.put("key3",jsonNode.path("data").path("key3").asText());resultMap.put("code",jsonNode.path("errCode").asText());}else{resultMap.put("code",jsonNode.path("errCode").asText());}return resultMap;} catch (Exception e) {throw new RuntimeException(e);}}