原文地址:

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. Jquery 之 日常积累(一)

    1.jquery函数在参数中传递 this,正确的写法: //页面中用 GetString(this); //脚本中定义 function GetString(obj){ var str = $(ob ...

  2. Ubuntu 下安装VNC server

    尽管我们在大部分情况下用ssh登录Ubuntu服务器就好了,但是有时候我们的程序需要在图形界面下运行,这时我们就要用到vnc server这个软件了.在Ubuntu下安装vnc server很简单的, ...

  3. Debian 7 升级内核

    Debian 7(wheezy)的内核是3.2,要想把内核升级到3.16怎么办呢?使用backports源! 一.添加backports源 打开/etc/apt/source.list文件,加入以下: ...

  4. SRM 584 第一次玩TopCoder。。。只水题一道。。。

    第一次topcoder,以前老感觉没有资格去做tc,cf什么的,现在已经慢慢接触了. 感觉还可以,还是有让我们这些蒻菜安慰的水题. tc的确很好玩,用客户端比赛,还有各种规则,而且还是只编写一个类提交 ...

  5. XShell提示Connection closed by foreign host的问题 和 路由器分配IP的规则

    情况是这样的: VMware中有三个Linux机器分别是crxy99(192.168.1.99),crxy100(192.168.1.100),crxy101(192.168.1.101),crxy1 ...

  6. hdu 1892 树状数组

    思路:就是一个很普通的二维树状数组,注意的是x1,y1不一定在x2,y2的左下方. #include<iostream> #include<cstring> #include& ...

  7. Angular 2.0 从0到1:Rx--隐藏在Angular 2.x中利剑

    第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...

  8. MongoDB - Installing MongoDB on Windows

    1. 在 http://www.mongodb.org/downloads 选择下载所需的版本. 2. 执行 msi 安装包,可通过 Custom 选项,选择安装目录. 3. 创建数据目录.Mongo ...

  9. Nginx - Windows 环境安装 Nginx

    1. 访问 http://nginx.org/en/download.html,下载 Windows 版本的安装包 2. 解压安装包,双击 nginx.exe,启动 nginx 3. 访问 http: ...

  10. Android OOM 解决方案

    Out of Memory(内存溢出) 几乎是每个Android程序员都会遇到的事.在网上也能找到一大堆的解决方案,之前写过一篇<Android 内存溢出管理与测试>的博文.但感觉写得不是 ...