通过 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. ...
随机推荐
- emouse思·睿—评论与观点整理之一
虽说我主要做的硬件,平时的兴趣爱好比较关注移动互联网,混迹于虎嗅.爱范儿.雷锋网.36Kr.cnBeta.瘾科技.i黑马.TechWeb等这类科技以及创业媒体,遗憾的是系统的去写的并不多,好在还算充分 ...
- [leetcode]Median of Two Sorted Arrays @ Python
原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...
- vRealize Automation部署虚机如果出错怎么办?
以下地方的日志可以查看: 1. Requests –> Choose my request -> View Detail –> Execution Information. 2. I ...
- Maven镜像更换为阿里云中央仓库
前言 maven仓库默认在国外,使用难免很慢,尤其是下载依赖的时候,换为国内镜像,让你感受飞一般的感觉.国内支持maven镜像的有阿里云,开源中国等,这里换为阿里云的. 更换 修改maven配置文件s ...
- 【中文分词系列】 4. 基于双向LSTM的seq2seq字标注
http://spaces.ac.cn/archives/3924/ 关于字标注法 上一篇文章谈到了分词的字标注法.要注意字标注法是很有潜力的,要不然它也不会在公开测试中取得最优的成绩了.在我看来,字 ...
- Spark Structured Streaming:将数据落地按照数据字段进行分区方案
方案一(使用ForeachWriter Sink方式): val query = wordCounts.writeStream.trigger(ProcessingTime(5.seconds)) . ...
- Dubbo框架应用之(三)--Zookeeper注冊中心、管理控制台的安装及解说
我是在linux下使用dubbo-2.3.3以上版本号的zookeeper注冊中心客户端. Zookeeper是Apache Hadoop的子项目,强度相对较好,建议生产环境使用该注冊中心. Dubb ...
- TCP/IP具体解释学习笔记--TCP的超时与重传
1.基本概念 TCP之所以能够安全的将数据在传输中的安全性,是因为它每次给对方发送数据,都会等待对方给个确认,当长时间收不到这个确认,发送端就会重发这个数据. 2.超时时间的測量 要測超时时间,TCP ...
- OpenGL ES 3.0之Shader and program(七)
着色器对象和程序对象是使用着色器渲染的2种基本的对象类型.一个着色器对象可以当做是一个C编译器,而程序对象作为连接器.一个编译器生成目标代码(如.OBJ,.o文件),对象文件完成创建后,C连接器将该对 ...
- (纪录片)《星际穿越》中的科学 The Science of Interstellar
简介: 导演: Gail Willumsen编剧: Gail Willumsen主演: 克里斯托弗·诺兰 / 乔纳森·诺兰 / 基普·索恩 / 马修·麦康纳类型: 纪录片 / 短片制片国家/地区: 美 ...