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 请求的更多相关文章

  1. java apache commons HttpClient发送get和post请求的学习整理(转)

    文章转自:http://blog.csdn.net/ambitiontan/archive/2006/01/06/572171.aspx HttpClient 是我最近想研究的东西,以前想过的一些应用 ...

  2. 使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL

    这里使用的是HttpComponents-Client-4.1.2 package com.jadyer.util; import java.io.File; import java.io.FileI ...

  3. springboot2.X集成HttpClient 发送HTTPS 请求

    1)jar <!--httpclient 发送外部https/http 请求--> <dependency> <groupId>org.apache.httpcom ...

  4. 用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 ...

  5. Java使用Apache的HttpClient组件发送https请求

    如果我们直接通过普通的方式对https的链接发送请求,会报一个如下的错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.Va ...

  6. java httpclient发送json 请求 ,go服务端接收

    /***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...

  7. 【JAVA】通过HttpClient发送HTTP请求的方法

    HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...

  8. org.apache.commons.httpclient工具类

    import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpcl ...

  9. httpClient使用中报错org.apache.commons.httpclient.HttpMethodBase - Going to buffer response body of large or unknown size.

    在使用HttpClient发送请求,使用httpMethod.getResponseBodyAsString();时当返回值过大时会报错: org.apache.commons.httpclient. ...

随机推荐

  1. java 生成zip文件并导出

    总结一下,关于Java下载zip文件并导出的方法,浏览器导出. String downloadName = "下载文件名称.zip"; downloadName = Browser ...

  2. Anaconda需要添加的环境变量

    F:\Anaconda3 F:\Anaconda3\Scripts F:\Anaconda3\Library\bin

  3. InnoDB Record, Gap, and Next-Key Locks

    InnoDB has several types of record-level locks including record locks, gap locks, and next-key locks ...

  4. Java调用DLL有多种方式,常用的方式有JNative、JNA、JNI等。

    JNative方式调用dll JNative是一种能够使Java语言使调用DLL的一种技术,对JNI进行了封装,可能有些读者会有这样一个问题,JNative对JNI进行了封装,并且是一种跨语言的使用D ...

  5. 【UOJ Round #3】

    枚举/二分 C题太神窝看不懂…… 核聚变反应强度 QwQ很容易发现次小的公约数一定是gcd的一个约数,然后……我就傻逼地去每次算出a[1],a[i]的gcd,然后枚举约数……这复杂度……哦呵呵... ...

  6. JUnit 入门

    JUNIT了解学习 转自:关于Java单元测试,你需要知道的一切 转自:JUnit 入门教程 JUnit高级用法之@RunWith @RunWith 关于@RunWith注解,官方文档是这么描述的: ...

  7. ASP.NET网页中RAR、DOC、PDF等文件下载功能实例源代码

    以前做asp.net下载功能的时候都是采用:<a href="http://www.wang0214.com/wgcms">下载</a>的方式来实现下载. ...

  8. 超链接a标签的href与onclick中使用javascript的区别

    onclick中javascript的区别一般没用到都没注意,但出错时才有些郁闷,看文本章解释如下: 以前一直很随意,后来看.net里的linkbutton似乎是用在<a href=" ...

  9. assetbundle 对自定义shader的打包

    http://docs.unity3d.com/Manual/managingassetdependencies.html Managing asset dependencies   Any give ...

  10. (转)unity使用line renderer画线

    原文地址:http://www.xuanyusong.com/archives/561 任何一个无规则曲线它都是有若干个线段组成,及时是圆形它也是又若干个线段组成的,也就是说将若干个线段拼接起来就是我 ...