com.founder.mip.utils.HttpUtil Maven / Gradle / Ivy
package com.founder.mip.utils;
import com.founder.core.log.MyLog;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.*;
public class HttpUtil {
private static final MyLog LOG = MyLog.getLog(HttpUtil.class);
//去掉https证书校验 2022-11-08
static {
try {
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
});
} catch (Exception e) {
}
}
public static String postData(String reqUrl, String reqData) throws IOException {
InputStream inputStream = null;
HttpURLConnection connection = null;
String result = null;
try {
connection = getHttpURLConnection(reqUrl, reqData);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
inputStream = connection.getInputStream();
byte[] bytes = new byte[1024];
int readBytes;
while ((readBytes = inputStream.read(bytes)) != -1) {
byteArrayOutputStream.write(bytes, 0, readBytes);
}
bytes = byteArrayOutputStream.toByteArray();
result = new String(bytes, "utf-8");
LOG.debug("请求结果:" + result);
} catch (Exception var12) {
var12.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
}
if (connection != null) {
connection.disconnect();
}
return result;
}
}
private static HttpURLConnection getHttpURLConnection(String reqUrl, String reqData) throws IOException {
URL url = new URL(reqUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
getOutputStream(connection, reqData);
return connection;
}
private static OutputStream getOutputStream(HttpURLConnection connection, String reqData) throws IOException {
OutputStream os = null;
try {
os = connection.getOutputStream();
os.write(reqData.getBytes());
os.flush();
} catch (Exception var8) {
var8.printStackTrace();
} finally {
if (os != null) {
os.close();
}
return os;
}
}
private static void trustAllHttpsCertificates() throws NoSuchAlgorithmException, KeyManagementException {
TrustManager[] trustAllCerts = new TrustManager[1];
trustAllCerts[0] = new TrustAllManager();
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
private static class TrustAllManager implements X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
}
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
}
}
}