package com.ruoyi.zncc.utils;
import lombok.extern.slf4j.Slf4j;
import javax.net.ssl.*;
import java.io.*;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
@Slf4j
public class HttpsClient {
private static final class DefaultTrustMannager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
public static HttpsURLConnection getHttpsURLConnection(String uri, String method) throws IOException, NoSuchAlgorithmException {
SSLContext ctx;
// try {
// ctx = SSLContext.getInstance("TLS");
// ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustMannager() }, new SecureRandom());
// } catch (NoSuchAlgorithmException | KeyManagementException e) {
// throw new RuntimeException(e);
// }
try {
ctx = SSLContext.getInstance("SSL");
TrustManager[] trustManagers = new TrustManager[1];
trustManagers[0] = new DefaultTrustMannager();
ctx.init(null, trustManagers, null);
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
} catch (KeyManagementException e) {
throw new RuntimeException(e);
}
SSLSocketFactory ssf = ctx.getSocketFactory();
URL url = new URL(uri);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setSSLSocketFactory(ssf);
urlConnection.setHostnameVerifier((s, sslSession) -> true);
urlConnection.setRequestMethod(method);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
// urlConnection.setConnectTimeout(10000);
if (method.equals("POST")) {
urlConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
}
return urlConnection;
}
public static byte[] getBytesFromStream(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] kb = new byte[1024];
int len;
while ((len = is.read(kb)) != -1) {
baos.write(kb, 0, len);
}
byte[] bytes = baos.toByteArray();
baos.close();
is.close();
return bytes;
}
private static void setBytesToStream(OutputStream os, byte[] bytes) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
byte[] kb = new byte[1024];
int len;
while ((len = bais.read(kb)) != -1) {
os.write(kb, 0, len);
}
os.flush();
os.close();
bais.close();
}
private static String doGet(String uri) {
try {
log.info("https的get请求链接为" + uri);
HttpsURLConnection httpsConn = getHttpsURLConnection(uri, "GET");
return new String(getBytesFromStream(httpsConn.getInputStream()));
} catch (IOException | NoSuchAlgorithmException e) {
log.error("https的请求出现异常:", e.getCause());
}
return null;
}
public static String doJsonPost(String uri, String data) {
try {
log.info("https的POST请求地址为:" + uri);
log.info("https的POST请求参数为:" + data);
HttpsURLConnection httpsConn = getHttpsURLConnection(uri, "POST");
setBytesToStream(httpsConn.getOutputStream(), data.getBytes("utf-8"));
String result = new String(getBytesFromStream(httpsConn.getInputStream()));
log.info("https的POST请求结果为:" + result);
return result;
} catch (IOException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
Java后台https调用远程接口取消ssl验证
最新推荐文章于 2023-11-16 10:32:41 发布