HttpClient SSL示例(转)
原文地址:
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示例(转)的更多相关文章
- [HttpClient]SSL双向实例
package com.jerry.httpclient; import java.io.File; import java.io.FileInputStream; import java.secur ...
- .NET Core 使用 HttpClient SSL 请求出错的解决办法
问题 使用 HTTP Client 请求 HTTPS 的 API 时出现 The certificate cannot be verified up to a trusted certificatio ...
- HttpClient使用示例
1)使用HttpClient发送GET请求 public class MainActivity extends Activity implements OnClickListener { privat ...
- 解决: httpclient ssl 验证导致死锁问题
线上图片下载服务器平时运行正常,最近突然出现一种比较奇怪的现象,只接受请求,但却没有处理请求,最开始怀疑下载线程挂掉了,dump 项目线程后发现异常: "pool-2-thread-1&qu ...
- HttpClient SSL connection could not be established error
系统从.net framework 升级到dotnet core2.1 原先工作正常的httpclient,会报SSL connection could not be established erro ...
- [转][C#]HttpClient 代码示例
转自:https://www.cnblogs.com/amosli/p/3918538.html 也参考了:https://www.cnblogs.com/ShadowFiend007/p/80668 ...
- angular5 httpclient的示例实战
摘要: 从angular 4.3.0 以后的版本开始使用httpclient,替换了之前的http,引用的包路径已经变为了angular/common/http了 一个基础的 httpclient 样 ...
- HttpClient4.5 SSL访问工具类
要从网上找一个HttpClient SSL访问工具类太难了,原因是HttpClient版本太多了,稍有差别就不能用,最后笔者干脆自己封装了一个访问HTTPS并绕过证书工具类. 主要是基于新版本Http ...
- Table of Contents - HttpClient
HttpClient 4.3.5 Getting Started HttpClient 简单示例 Fundamentals Request Execution HTTP Request & H ...
随机推荐
- Word2010编号列表&多级列表
1.引用场景 对于一份标准.漂亮的word文档,编号列表和多级列表的设置时必不可少的,正因为有它们,文档看起来才更专业,使用起来才更加的方便.如下面截图一般,这是十分常见的多级列表设置 ...
- C#多线程的介绍(园子里比较全的一篇)
一.多线程的概念 Windows是一个多任务的系统,如果你使用的是windows 2000及其以上版本,你可以通过任务管理器查看当前系统运行的程序和进程.什么是进程呢?当一个程序开始运行时,它就是一 ...
- 115 Java Interview Questions and Answers – The ULTIMATE List--reference
In this tutorial we will discuss about different types of questions that can be used in a Java inter ...
- 构建高效安全的Nginx Web服务器
一 为什么选择Nginx搭建Web服务器 Apache和Nginx是目前使用最火的两种Web服务器,Apache出现比Nginx早.Apache HTTP Server(简称Apache)是世界使用排 ...
- const int * pi/int * const pi的区别
前面有一篇文章:数组名就是常量指针 参考文章:http://blog.pfan.cn/whyhappy/5164.html const int * pi .int const * pi与int * ...
- 小白日记40:kali渗透测试之Web渗透-SQL手工注入(二)-读取文件、写入文件、反弹shell
SQL手工注入 1.读取文件[load_file函数] ' union SELECT null,load_file('/etc/passwd')--+ burpsuite 2.写入文件 ' unio ...
- 炼数成金hadoop视频干货03
视频地址:http://pan.baidu.com/s/1dDEgKwD 着重介绍了HDFS 运行了示例程序wordcount,自己也试了一遍(用的伪分布式) 1.建立数据(和讲师的操作有些不一样,不 ...
- Android(java)学习笔记78:设计模式之单例模式
单例模式代码示例: 1. 单例模式之饿汉式: package cn.itcast_03; public class Student { // 构造私有 private Student() { } // ...
- learning nodejs 2 - connect middleware
学习了connect module nodejs 的中间件方式 var connect = require('connect'); var server = connect.createServer( ...
- vb中的null.nothing.empty区别
以下内容源自互联网: 变量 A.B.C.D 分别等于 0."".Null. Empty. Nothing 的哪一个? Dim A Dim B As String Dim C As ...