通过 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. ...
随机推荐
- Eclipse SDK Android Studio 下载地址
https://developer.android.com/sdk/index.html#download 这个网址可以下载需要的东西,FQ的话可以给 xifulinmen@gmail.com 发一个 ...
- [Android Pro] AndroidStudio IDE界面插件开发(进阶篇之Editor)
转载请注明出处:[huachao1001的专栏:http://blog.csdn.net/huachao1001/article/details/53885981] 我们开发AndroidStudio ...
- [Hook] 免root,自己进程内,binder hook (ClipboardManager)
cp from : http://weishu.me/2016/02/16/understand-plugin-framework-binder-hook/ Android系统通过Binder机制给应 ...
- Memcache的安装与配置
因为单位要求修复Memcached的DDOS漏洞,整理了本文.之前的文章防止Memcached的DDOS攻击另外一个思路 提到了解决方案,我们使用的版本较低,因此需要对 Memcached 进行升级, ...
- Linux 批量查找替换方法(VIM和sed)
版权声明:欢迎与我交流讨论,若要转载请注明出处~ https://blog.csdn.net/sinat_36053757/article/details/70946263 1.VIM命令 当前行进行 ...
- 使用Bootstrap后,关于IE与Chrome显示字体的问题
在做日志系统时,使用了Bootstrap,然后通过浏览器查看的页面效果如下 对比可以看到,同样的字体,IE显示的圆润些,而Chrome字体则丑很多.因为Chrome默认用宋体 在http://v3.b ...
- spring 配置文件 获取变量(PropertyPlaceholderConfigurer)
转自:https://hbiao68.iteye.com/blog/2031006 1.Spring的框架中,org.springframework.beans.factory.config.Prop ...
- c语言统计程序执行时间
c语言程序执行时间 #include <iostream> #include <cstdio> #include <ctime> int main() { std: ...
- bash shell redirecting code block
参考了:https://www.cnblogs.com/sparkdev/p/10247187.html https://www.tldp.org/LDP/abs/html/redircb.html ...
- ES8新特性——ES8 was Released and here are its Main New Features
原文: https://hackernoon.com/es8-was-released-and-here-are-its-main-new-features-ee9c394adf66 -------- ...