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);
}

最新文章

  1. viewPager的基本使用
  2. 说说Web API数据格式化——Json
  3. java parseint()
  4. DM8168 编译filesystem步骤
  5. 关于svn获取获取文件时 Unable to connect to a repository at URL&quot;https://...&quot;执行上下文错误:参数错误
  6. OpenStack最新版本Folsom架构解析
  7. 抛掉kendoUI的MultiSelect,自己实现 DropDownList MultiSelect
  8. 阮一峰:MVC、MVP和MVVM的图示
  9. Java String类和Object类
  10. node--更新数据库问题
  11. Lua语法要点
  12. DIN(Deep Interest Network of CTR) [Paper笔记]
  13. 基于低代码平台(Low Code Platform)开发中小企业信息化项目
  14. 【二分+容斥+莫比乌斯反演】BZOJ2440 完全平方数
  15. (最完美)MIUI12系统的Usb调试模式在哪里开启的步骤
  16. 开源流媒体服务器SRS学习笔记(3) - HTTPCallback实现安全认证
  17. 洛谷.1110.[ZJOI2007]报表统计(Splay Heap)
  18. phing
  19. android控件跟随手势滑动改变位置
  20. 如何弹出QQ临时对话框实现不添加好友在线交谈效果

热门文章

  1. [Web前端] 给li设置float浮动属性之后,无法撑开外层ul的问题。
  2. 使用kubectl创建部署
  3. PHP 5.4.17 发布!
  4. aspnet_regiis -i VS 20XX 的开发人员命令提示符
  5. [leetcode]Edit Distance @ Python
  6. Qt OpenGL:学习现代3D图形编程之四,透视投影浅析
  7. git pull fails “unable to resolve reference” “unable to update local ref”
  8. 【架构】技术-工具-平台-语言&amp;框架
  9. async和await的返回值——NodeJS, get return value from async await
  10. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十三)kafka+spark streaming打包好的程序提交时提示虚拟内存不足(Container is running beyond virtual memory limits. Current usage: 119.5 MB of 1 GB physical memory used; 2.2 GB of 2.1 G)