|
|
package com.example.zxweb.utils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.http.*;
|
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
|
import org.springframework.http.converter.StringHttpMessageConverter;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 调用 Restful 接口 Util
|
|
|
*
|
|
|
* @author sunjianlei
|
|
|
*/
|
|
|
@Slf4j
|
|
|
public class RestUtil {
|
|
|
|
|
|
private static String domain = null;
|
|
|
private static String path = null;
|
|
|
|
|
|
/*private static String getDomain() {
|
|
|
if (domain == null) {
|
|
|
domain = SpringContextUtils.getDomain();
|
|
|
// issues/2959
|
|
|
// 微服务版集成企业微信单点登录
|
|
|
// 因为微服务版没有端口号,导致 SpringContextUtils.getDomain() 方法获取的域名的端口号变成了:-1所以出问题了,只需要把这个-1给去掉就可以了。
|
|
|
String port=":-1";
|
|
|
if (domain.endsWith(port)) {
|
|
|
domain = domain.substring(0, domain.length() - 3);
|
|
|
}
|
|
|
}
|
|
|
return domain;
|
|
|
}
|
|
|
|
|
|
private static String getPath() {
|
|
|
if (path == null) {
|
|
|
path = SpringContextUtils.getApplicationContext().getEnvironment().getProperty("server.servlet.context-path");
|
|
|
}
|
|
|
return oConvertUtils.getString(path);
|
|
|
}
|
|
|
|
|
|
public static String getBaseUrl() {
|
|
|
String basepath = getDomain() + getPath();
|
|
|
log.info(" RestUtil.getBaseUrl: " + basepath);
|
|
|
return basepath;
|
|
|
}*/
|
|
|
|
|
|
/**
|
|
|
* RestAPI 调用器
|
|
|
*/
|
|
|
private static RestTemplate RT;
|
|
|
|
|
|
static {
|
|
|
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
|
|
|
requestFactory.setConnectTimeout(30*1000);
|
|
|
requestFactory.setReadTimeout(30*1000);
|
|
|
RT = new RestTemplate(requestFactory);
|
|
|
// 解决乱码问题
|
|
|
RT.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
|
|
|
}
|
|
|
|
|
|
public static RestTemplate getRestTemplate() {
|
|
|
return RT;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送 get 请求
|
|
|
*/
|
|
|
public static JSONObject get(String url) {
|
|
|
return getNative(url, null, null).getBody();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送 get 请求
|
|
|
*/
|
|
|
public static JSONObject get(String url, JSONObject variables) {
|
|
|
return getNative(url, variables, null).getBody();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送 get 请求
|
|
|
*/
|
|
|
public static JSONObject get(String url, JSONObject variables, JSONObject params) {
|
|
|
return getNative(url, variables, params).getBody();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送 get 请求,返回原生 ResponseEntity 对象
|
|
|
*/
|
|
|
public static ResponseEntity<JSONObject> getNative(String url, JSONObject variables, JSONObject params) {
|
|
|
return request(url, HttpMethod.GET, variables, params,null);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送 Post 请求
|
|
|
*/
|
|
|
public static JSONObject post(String url) {
|
|
|
return postNative(url, null, null,null).getBody();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送 Post 请求
|
|
|
*/
|
|
|
public static JSONObject post(String url, JSONObject params,JSONObject headers) {
|
|
|
return postNative(url, null, params,headers).getBody();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送 Post 请求
|
|
|
*/
|
|
|
public static JSONObject post(String url, JSONObject variables, JSONObject params,JSONObject headers) {
|
|
|
return postNative(url, variables, params,headers).getBody();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送 POST 请求,返回原生 ResponseEntity 对象
|
|
|
*/
|
|
|
public static ResponseEntity<JSONObject> postNative(String url, JSONObject variables, JSONObject params,JSONObject headers) {
|
|
|
return request(url, HttpMethod.POST, variables, params,headers);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送 put 请求
|
|
|
*/
|
|
|
public static JSONObject put(String url) {
|
|
|
return putNative(url, null, null,null).getBody();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送 put 请求
|
|
|
*/
|
|
|
public static JSONObject put(String url, JSONObject params) {
|
|
|
return putNative(url, null, params,null).getBody();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送 put 请求
|
|
|
*/
|
|
|
public static JSONObject put(String url, JSONObject variables, JSONObject params,JSONObject headers) {
|
|
|
return putNative(url, variables, params,headers).getBody();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送 put 请求,返回原生 ResponseEntity 对象
|
|
|
*/
|
|
|
public static ResponseEntity<JSONObject> putNative(String url, JSONObject variables, JSONObject params,JSONObject headers) {
|
|
|
return request(url, HttpMethod.PUT, variables, params,headers);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送 delete 请求
|
|
|
*/
|
|
|
public static JSONObject delete(String url) {
|
|
|
return deleteNative(url, null, null).getBody();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送 delete 请求
|
|
|
*/
|
|
|
public static JSONObject delete(String url, JSONObject variables, JSONObject params) {
|
|
|
return deleteNative(url, variables, params).getBody();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送 delete 请求,返回原生 ResponseEntity 对象
|
|
|
*/
|
|
|
public static ResponseEntity<JSONObject> deleteNative(String url, JSONObject variables, JSONObject params) {
|
|
|
return request(url, HttpMethod.DELETE, null, variables, params, JSONObject.class);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送请求
|
|
|
*/
|
|
|
public static ResponseEntity<JSONObject> request(String url, HttpMethod method, JSONObject variables, JSONObject params,JSONObject header) {
|
|
|
HttpHeaders headers=null;
|
|
|
if (header==null) {
|
|
|
headers = getHeaderApplicationJson();
|
|
|
}else{
|
|
|
headers= new HttpHeaders();
|
|
|
for (String s : header.keySet()) {
|
|
|
/*if ("Authorization".equals(s)) {+
|
|
|
JSONObject jsonObject = header.getJSONObject(s);
|
|
|
String username = jsonObject.getString("username");
|
|
|
String password = jsonObject.getString("password");
|
|
|
String auth = username + ":" + password;
|
|
|
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
|
|
|
headers.add(org.apache.hc.core5.http.HttpHeaders.AUTHORIZATION,"Basic " + encodedAuth);
|
|
|
continue;
|
|
|
}*/
|
|
|
headers.add(s,header.getString(s));
|
|
|
}
|
|
|
}
|
|
|
String cookie =(String) CacheManager.get("Cookie");
|
|
|
if (StringUtils.isNotBlank(cookie)) {
|
|
|
headers.add("Cookie",cookie);
|
|
|
}
|
|
|
return request(url, method, headers, variables, params, JSONObject.class);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送请求
|
|
|
*
|
|
|
* @param url 请求地址
|
|
|
* @param method 请求方式
|
|
|
* @param headers 请求头 可空
|
|
|
* @param variables 请求url参数 可空
|
|
|
* @param params 请求body参数 可空
|
|
|
* @param responseType 返回类型
|
|
|
* @return ResponseEntity<responseType>
|
|
|
*/
|
|
|
public static <T> ResponseEntity<T> request(String url, HttpMethod method, HttpHeaders headers, JSONObject variables, Object params, Class<T> responseType) {
|
|
|
log.info(" RestUtil --- request --- url = "+ url);
|
|
|
if (StringUtils.isEmpty(url)) {
|
|
|
throw new RuntimeException("url 不能为空");
|
|
|
}
|
|
|
if (method == null) {
|
|
|
throw new RuntimeException("method 不能为空");
|
|
|
}
|
|
|
if (headers == null) {
|
|
|
headers = new HttpHeaders();
|
|
|
}
|
|
|
// 请求体
|
|
|
String body = "";
|
|
|
if (params != null) {
|
|
|
if (params instanceof JSONObject) {
|
|
|
body = ((JSONObject) params).toJSONString();
|
|
|
|
|
|
} else {
|
|
|
body = params.toString();
|
|
|
}
|
|
|
}
|
|
|
// 拼接 url 参数
|
|
|
if (variables != null && !variables.isEmpty()) {
|
|
|
url += ("?" + asUrlVariables(variables));
|
|
|
}
|
|
|
try {
|
|
|
SslUtils.ignoreSsl();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
log.info("打印headers:");
|
|
|
log.info("-----------");
|
|
|
for (String s : headers.keySet()) {
|
|
|
log.info(s+":"+headers.get(s));
|
|
|
}
|
|
|
log.info("-----------");
|
|
|
// 发送请求
|
|
|
HttpEntity<String> request = new HttpEntity<>(body, headers);
|
|
|
//回调接口
|
|
|
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
|
|
|
requestFactory.setConnectTimeout(300 *1000);
|
|
|
requestFactory.setReadTimeout(300* 1000);
|
|
|
RT = new RestTemplate(requestFactory);
|
|
|
// 解决乱码问题
|
|
|
RT.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
|
|
|
ResponseEntity<T> exchange = RT.exchange(url, method, request, responseType);
|
|
|
HttpHeaders responseBodyHeaders = exchange.getHeaders();
|
|
|
List<String> stringList = responseBodyHeaders.get("Set-Cookie");
|
|
|
StringBuilder sb=new StringBuilder();
|
|
|
for (String s : stringList) {
|
|
|
if (s.contains("JSESSIONID") &&!s.contains("deleteMe")) {
|
|
|
sb.append(s);
|
|
|
}
|
|
|
}
|
|
|
if (StringUtils.isNotBlank(sb.toString())) {
|
|
|
CacheManager.put("Cookie",sb.toString());
|
|
|
}
|
|
|
return RT.exchange(url, method, request, responseType);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取JSON请求头
|
|
|
*/
|
|
|
public static HttpHeaders getHeaderApplicationJson() {
|
|
|
return getHeader(MediaType.APPLICATION_JSON_UTF8_VALUE);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取请求头
|
|
|
*/
|
|
|
public static HttpHeaders getHeader(String mediaType) {
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.setContentType(MediaType.parseMediaType(mediaType));
|
|
|
headers.add("Accept", mediaType);
|
|
|
return headers;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将 JSONObject 转为 a=1&b=2&c=3...&n=n 的形式
|
|
|
*/
|
|
|
public static String asUrlVariables(JSONObject variables) {
|
|
|
Map<String, Object> source = variables.getInnerMap();
|
|
|
Iterator<String> it = source.keySet().iterator();
|
|
|
StringBuilder urlVariables = new StringBuilder();
|
|
|
while (it.hasNext()) {
|
|
|
String key = it.next();
|
|
|
String value = "";
|
|
|
Object object = source.get(key);
|
|
|
if (object != null) {
|
|
|
if (!StringUtils.isEmpty(object.toString())) {
|
|
|
value = object.toString();
|
|
|
}
|
|
|
}
|
|
|
urlVariables.append("&").append(key).append("=").append(value);
|
|
|
}
|
|
|
// 去掉第一个&
|
|
|
return urlVariables.substring(1);
|
|
|
}
|
|
|
|
|
|
/*public static JSONObject postTest(String httpUrl,JSONObject requestBodyJson){
|
|
|
RestTemplate restTemplate = null;
|
|
|
try {
|
|
|
restTemplate = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory());
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (KeyManagementException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (KeyStoreException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
String result = HttpUtil.post(httpUrl, requestBodyJson.toJSONString());
|
|
|
//打开建立连接
|
|
|
log.debug("post_responseBody:");
|
|
|
log.debug(result);
|
|
|
//获取返回结果
|
|
|
return JSONObject.parseObject(result);
|
|
|
}*/
|
|
|
}
|