1、通過 HTTPS 發送 POST 請求;
2、HTTPS 安全協議采用 TLSv1.2;
3、 使用代理(Proxy)進行 HTTPS 訪問;
4、指定 Content-Type 為:application/x-www-form-urlencoded;
5、HTTPS 請求時加載客戶端證書(Client Certificate);
6、忽略服務器端證書鏈(Server Certificate Chain)的校驗(Validate)。
public static void main(String[] args) throws IOException, UnrecoverableKeyException, CertificateException, KeyStoreException, KeyManagementException { SSLConnectionSocketFactory socketFactory = getSocketFactory(); // 創建 CloseableHttpClient 對象 CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(socketFactory).build(); // 指定請求的 URL 並創建 HttpPost 對象 HttpPost httppost = new HttpPost("https://xxxx/yyyy"); // 設置請求通過的代理 httppost.setConfig(RequestConfig.custom().setProxy(new HttpHost("host", 8080)).build()); HttpEntity entity; // 設置請求的 ContentType 為 application/x-www-form-urlencoded httppost.addHeader(HttpHeaders.CONTENT_TYPE, Consts.HTTP_REQUEST_CONTENTTYPE_FORM); // 構建 POST 的內容 List<BasicNameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair("amount", "1.00")); entity = new UrlEncodedFormEntity(nvps, Consts.CHARSET_UTF8); httppost.setEntity(entity); CloseableHttpResponse response = null; try { // 發送請求 response = httpclient.execute(httppost); // 獲取響應內容 HttpEntity entity1 = response.getEntity(); System.out.println(EntityUtils.toString(entity1)); } finally { if (null != response) { response.close(); } if (null != httpclient) { httpclient.close(); } } } // 忽略服務器端證書鏈的認證 private static TrustManager getTrustManagers() { return new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } }; } private static SSLConnectionSocketFactory getSocketFactory() throws IOException, KeyStoreException, CertificateException, UnrecoverableKeyException, KeyManagementException { SSLContext sslContext; try { // keyStore 用來存放客戶端證書 KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File("d:\\test.p12")); try { keyStore.load(instream, "passwd".toCharArray()); } finally { instream.close(); } // 加載客戶端證書,並設置HTTPS的安全協議為 TLSv1.2 sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, "passwd".toCharArray()).useProtocol("TLSv1.2").build(); } catch (NoSuchAlgorithmException e) { return null; } try { sslContext.init(null, new TrustManager[]{getTrustManagers()}, new java.security.SecureRandom()); } catch (KeyManagementException e) { return null; } return new SSLConnectionSocketFactory(sslContext); }
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpUtil {
public static void main(String[] args) throws IOException, UnrecoverableKeyException, CertificateException, KeyStoreException, KeyManagementException {
SSLConnectionSocketFactory socketFactory = getSocketFactory();
// 創建 CloseableHttpClient 對象
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
// 指定請求的 URL 並創建 HttpPost 對象
HttpPost httppost = new HttpPost("https://www.baidu.com");
// 設置請求通過的代理
httppost.setConfig(RequestConfig.custom().setProxy(new HttpHost("host", 8080)).build());
HttpEntity entity;
// 設置請求的 ContentType 為 application/x-www-form-urlencoded
httppost.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
// 構建 POST 的內容
List<BasicNameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair("amount", "1.00"));
entity = new UrlEncodedFormEntity(nvps, "utf-8");
httppost.setEntity(entity);
CloseableHttpResponse response = null;
try {
// 發送請求
response = httpclient.execute(httppost);
// 獲取響應內容
HttpEntity entity1 = response.getEntity();
System.out.println(EntityUtils.toString(entity1));
} finally {
if (null != response) {
response.close();
}
if (null != httpclient) {
httpclient.close();
}
}
}
// 忽略服務器端證書鏈的認證
private static TrustManager getTrustManagers() {
return new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
};
}
private static SSLConnectionSocketFactory getSocketFactory() throws IOException, KeyStoreException, CertificateException, UnrecoverableKeyException, KeyManagementException {
SSLContext sslContext;
try {
// keyStore 用來存放客戶端證書
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File("d:\\test.p12"));
try {
keyStore.load(instream, "passwd".toCharArray());
} finally {
instream.close();
}
// 加載客戶端證書,並設置HTTPS的安全協議為 TLSv1.2
sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, "passwd".toCharArray()).useProtocol("TLSv1.2").build();
} catch (NoSuchAlgorithmException e) {
return null;
}
try {
sslContext.init(null, new TrustManager[]{getTrustManagers()}, new java.security.SecureRandom());
} catch (KeyManagementException e) {
return null;
}
return new SSLConnectionSocketFactory(sslContext);
}
}