通过 Apache Commons HttpClient 发送 HTTPS 请求
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);
}
通过 Apache Commons HttpClient 发送 HTTPS 请求的更多相关文章
- java apache commons HttpClient发送get和post请求的学习整理(转)
文章转自:http://blog.csdn.net/ambitiontan/archive/2006/01/06/572171.aspx HttpClient 是我最近想研究的东西,以前想过的一些应用 ...
- 使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL
这里使用的是HttpComponents-Client-4.1.2 package com.jadyer.util; import java.io.File; import java.io.FileI ...
- springboot2.X集成HttpClient 发送HTTPS 请求
1)jar <!--httpclient 发送外部https/http 请求--> <dependency> <groupId>org.apache.httpcom ...
- 用HttpClient发送HTTPS请求报SSLException: Certificate for <域名> doesn't match any of the subject alternative names问题的解决
最近用server酱-PushBear做消息自动推送,用apache HttpClient做https的get请求,但是代码上到服务器端就报javax.net.ssl.SSLException: Ce ...
- Java使用Apache的HttpClient组件发送https请求
如果我们直接通过普通的方式对https的链接发送请求,会报一个如下的错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.Va ...
- java httpclient发送json 请求 ,go服务端接收
/***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...
- 【JAVA】通过HttpClient发送HTTP请求的方法
HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...
- org.apache.commons.httpclient工具类
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpcl ...
- httpClient使用中报错org.apache.commons.httpclient.HttpMethodBase - Going to buffer response body of large or unknown size.
在使用HttpClient发送请求,使用httpMethod.getResponseBodyAsString();时当返回值过大时会报错: org.apache.commons.httpclient. ...
随机推荐
- git error: RPC failed; curl 56 GnuTLS recv error 解决方案
// git 报错情况: error: RPC failed; curl 56 GnuTLS recv error (-110): The TLS connection was non-properl ...
- TCP/IP协议体系结构简介
1.TCP/IP协议栈 四层模型 TCP/IP这个协议遵守一个四层的模型概念:应用层.传输层.互联层和网络接口层. 网络接口层:模型的基层是网络接口层.负责数据帧的发送和接收,帧是独立的网络信息传输单 ...
- 【没有注意过的细节】用scanf读一个unsigned char? %hhu 的用法
头段时间我的代码,有一个 unsigned char,我需要从sscanf 中读取字符串为这个值.但是一般char 是用%c的,我是要值得. 所以我使用了%d %u来读,结果报警告: unsigned ...
- android自定义风格的toast
先上图看一下我的自定义toast的样式 源码下载地址: CustomToastActivity.java源码 package com.jinhoward.ui_customtoast; /** * A ...
- [leetcode]Add Binary @ Python
原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (al ...
- [leetcode]Longest Consecutive Sequence @ Python
原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/ 题意: Given an unsorted array of i ...
- CSS-div高度100%设置问题
div常用的属性width和height,有的时候如果我们需要让div的高度是整个屏幕的高度,设置height:100%发现并没有什么作用,并不是这样设置不对,而是w3c规范中关于百分比的设置是相对于 ...
- 插件化 VirtualAPK 简介 体验 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 深入浅出Nodejs读书笔记
深入浅出Nodejs读书笔记 转:http://tw93.github.io/2015-03-01/shen-ru-qian-chu-nodejs-reading-mind-map.html cate ...
- Android WindowManager实现悬浮窗效果 (一)——与当前Activity绑定
最近有学生做毕业设计,想使用悬浮窗这种效果,其实很简单,我们可以通过系统服务WindowManager来实现此功能,本章我们来试验一下在当前Activity之上创建一个悬浮的view. 第一步:认识W ...