java发送HTTPS请求(远程调用接口,对接接口)
绕过ssl证书
情景描述:对接内网不同部门的接口,使用的是HTTPS,SSL证书不可信
参考地址:https://www.it610.com/article/1297347756607741952.html https://www.it610.com/article/1281277477364187136.html
package com.gzzn.credit.mass.util;
import com.gzzn.credit.mass.service.ElectronicCertificateService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public class SendHttpClientUtil {
public static Logger logger = Logger.getLogger(SendHttpClientUtil.class);
static {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
if (hostname.equals("127.0.0.1xx")) {
return true;
}
return false;
}
});
}
static HostnameVerifier _hv = new HostnameVerifier() {
@Override
public boolean verify(String host, SSLSession sslSession) {
logger.warn("Host===>" + host);
logger.warn("sslSession.getPeerHost====>" + sslSession.getPeerHost());
return true;
}
};
static TrustManager[] _trustCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
};
static {
try {
HttpsURLConnection.setDefaultHostnameVerifier(_hv);
SSLContext ctxSSL = null;
ctxSSL = SSLContext.getInstance("SSL");
ctxSSL.init(null, _trustCerts, null);
HttpsURLConnection.setDefaultSSLSocketFactory(ctxSSL.getSocketFactory());
} catch (Exception e) {
logger.error("SSL context set fail: {}", e);
}
}
public static String doPost(String urlStr, String json) {
return doPost(urlStr, json, "UTF-8");
}
public static String doPost(String urlStr, String json, String charset) {
String result = null;
logger.error("POST请求地址:------>" + urlStr);
logger.error("POST请求参数:------>" + json);
try {
DataOutputStream out = null;
BufferedReader reader = null;
HttpsURLConnection connection = null;
try {
URL url = new URL(urlStr);
connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
out = new DataOutputStream(connection.getOutputStream());
if (StringUtils.isNotBlank(json)) {
out.write(json.getBytes(charset));
}
out.flush();
out.close();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), charset));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes());
sb.append(lines);
}
result = sb.toString();
logger.error("请求返回结果:" + result);
reader.close();
connection.disconnect();
} catch (Exception e) {
logger.error("发送post 请求失败--->", e);
} finally {
if (out != null) {
out.flush();
out.close();
}
if (reader != null) {
reader.close();
}
if (connection != null) {
connection.disconnect();
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("发送post 请求失败--->", e);
}
return result;
}
public static String doGet(String urlStr) {
return doGet(urlStr, "UTF-8");
}
public static String doGet(String urlStr, String charset) {
String result = null;
logger.error("GET请求地址:------>" + urlStr);
try {
BufferedReader reader = null;
HttpsURLConnection connection = null;
try {
URL url = new URL(urlStr);
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), charset));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes());
sb.append(lines);
}
result = sb.toString();
logger.error("请求返回结果:" + result);
reader.close();
connection.disconnect();
} catch (Exception e) {
logger.error("发送get 请求失败--->", e);
} finally {
if (reader != null) {
reader.close();
}
if (connection != null) {
connection.disconnect();
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("发送get 请求失败--->", e);
}
return result;
}
}