原文地址:

http://www.cnblogs.com/jerry19890622/p/4291053.html

package com.jerry.httpclient;

import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore; import javax.net.ssl.SSLContext; import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; /**
*
* @author Jerry
* @date 2015年2月12日 下午11:38:33
*/
public class SSLDemo { public static final String KEY_STORE_TYPE_JKS = "jks";
public static final String KEY_STORE_TYPE_P12 = "PKCS12"; public static void main(String[] args) throws Exception{
KeyStore keyStore = KeyStore.getInstance(KEY_STORE_TYPE_P12);//服务器发给浏览器的KeyStore
KeyStore trustStore = KeyStore.getInstance(KEY_STORE_TYPE_JKS);//浏览器发给服务器的KeyStore
FileInputStream input = new FileInputStream(new File("d:\\tomcat.keystore"));
FileInputStream keyInput = new FileInputStream("d:\\mykey.p12");
trustStore.load(input, "123456".toCharArray());
keyStore.load(keyInput, "123456".toCharArray());
/*
* 使用2个keystore创建sslcontext
*/
SSLContext sslContext = SSLContexts.custom().useTLS().
loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, "123456".toCharArray())
.build();
//通过sllcontext创建一个socket factory
SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(sslFactory).build();
HttpGet get = new HttpGet("https://localhost:8443/services");
System.out.println("executing request: " + get.getRequestLine());
CloseableHttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("response content lenght: " + entity.getContentLength());
}
System.out.println(EntityUtils.toString(entity));
EntityUtils.consume(entity);
}
}

HttpClient SSL示例(转)的更多相关文章

  1. [HttpClient]SSL双向实例

    package com.jerry.httpclient; import java.io.File; import java.io.FileInputStream; import java.secur ...

  2. .NET Core 使用 HttpClient SSL 请求出错的解决办法

    问题 使用 HTTP Client 请求 HTTPS 的 API 时出现 The certificate cannot be verified up to a trusted certificatio ...

  3. HttpClient使用示例

    1)使用HttpClient发送GET请求 public class MainActivity extends Activity implements OnClickListener { privat ...

  4. 解决: httpclient ssl 验证导致死锁问题

    线上图片下载服务器平时运行正常,最近突然出现一种比较奇怪的现象,只接受请求,但却没有处理请求,最开始怀疑下载线程挂掉了,dump 项目线程后发现异常: "pool-2-thread-1&qu ...

  5. HttpClient SSL connection could not be established error

    系统从.net framework 升级到dotnet core2.1 原先工作正常的httpclient,会报SSL connection could not be established erro ...

  6. [转][C#]HttpClient 代码示例

    转自:https://www.cnblogs.com/amosli/p/3918538.html 也参考了:https://www.cnblogs.com/ShadowFiend007/p/80668 ...

  7. angular5 httpclient的示例实战

    摘要: 从angular 4.3.0 以后的版本开始使用httpclient,替换了之前的http,引用的包路径已经变为了angular/common/http了 一个基础的 httpclient 样 ...

  8. HttpClient4.5 SSL访问工具类

    要从网上找一个HttpClient SSL访问工具类太难了,原因是HttpClient版本太多了,稍有差别就不能用,最后笔者干脆自己封装了一个访问HTTPS并绕过证书工具类. 主要是基于新版本Http ...

  9. Table of Contents - HttpClient

    HttpClient 4.3.5 Getting Started HttpClient 简单示例 Fundamentals Request Execution HTTP Request & H ...

随机推荐

  1. Python学习 之 函数

    1.为什么要使用函数 (1)降低编程难度:将复杂的问题分解成简单的小问题 (2)代码重用 2.函数的定义 def 函数名(参数列表):#可以没有参数 函数体 3.函数缺省参数(默认参数):设置默认参数 ...

  2. PHP.2-LAMP平台介绍及网站的工作原理

    LAMP平台介绍及网站的工作原理 1.HTTP协议 URL(UniformResourceLocator)统一资源定位符,就是网页地址的意思.[格式:协议://主机.端口.文件.附加资源] ##URL ...

  3. Jquery小实例

    1正反选 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  4. struts2.1笔记04:struts2优点

  5. The required Server component failed to start so Tomcat is unable to start解决之一

    http://www.cnblogs.com/quxuedan/archive/2012/12/11/2813445.html 看看这个博客园园主说的吧

  6. [转]Maintain File Upload Control on Postbacks

    本文转自:http://www.ironspeed.com/articles/Maintain%20File%20Upload%20Control/Article.aspx Introduction ...

  7. [转]不用安装Oracle Client如何使用PLSQL Developer

    本文转自:http://www.cnblogs.com/sleepywang/archive/2009/10/13/1582654.html 1. 下载oracle的客户端程序包(30M) 只需要在O ...

  8. ZOJ 3209 Treasure Map (Dancing Links)

    Treasure Map Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit S ...

  9. LeetCode 264

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  10. 转:C++ 性能测试支持

    转: http://codinginet.com/articles/view/201606-use_gtestx_for_benchmark?simple=1&from=timeline&am ...