package com.yf.exam.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
public class IdmApiClient {
private static Logger LOGGER = LoggerFactory.getLogger(IdmApiClient.class);
public static JSONObject executessss(RequestBuilder builder) {
try {
CloseableHttpClient client = buildHttpClient();
HttpUriRequest request = builder.build();
LOGGER.debug("请求接口:{}", request);
CloseableHttpResponse response = client.execute(request);
String result = EntityUtils.toString(response.getEntity());
LOGGER.debug("接口响应:{}", result);
HttpClientUtils.closeQuietly(response);
HttpClientUtils.closeQuietly(client);
return JSON.parseObject(result);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String buildSign(RequestBuilder builder) {
String uri = StringUtils.substringAfter(builder.build().getURI().toString(), prefix);
String content = "";
String method = builder.getMethod();
if (StringUtils.equals(method, "PUT") || StringUtils.equals(method, "POST")) {
try {
HttpEntity entity = builder.getEntity();
if (Objects.nonNull(entity)) {
content = EntityUtils.toString(entity);
}
} catch (IOException ignored) {
}
}
String text = StringUtils.join(new String[]{uri, content, privateKey}, "&");
return DigestUtils.md5Hex(text);
}
private static CloseableHttpClient buildHttpClient() throws Exception {
SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial((TrustStrategy) (chain, authType) -> true)
.build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
sslcontext,
new String[]{"TLSv1"},
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
return HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
}
}
RequestBuilder builder = RequestBuilder.get("请求地址");
JSONObject getJson = IdmApiClient.executessss(builder);
System.out.println(getJson);
JSONObject object = new JSONObject();
object.put("title", "通知消息");
RequestBuilder builders = RequestBuilder.post("请求地址").setEntity(new StringEntity(object.toString(), ContentType.APPLICATION_JSON));
JSONObject postjson = IdmApiClient.executessss(builders);
System.out.println(postjson);
|