这里使用的是HttpComponents-Client-4.1.2

 package com.jadyer.util;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils; /**
* 使用HttpClient模拟HTTPS访问
* @see ===================================================================================================================================
* @see 【配置Tomcat支持SSL(即让Tomcat下的Web应用处于SSL安全通道中)】
* @see ===================================================================================================================================
* @see 1、生成KeyStore
* @see 1)运行-->CMD-->"keytool -genkey -alias Jadyer_SSL_20120508 -keyalg RSA -validity 1024 -keystore D:\Jadyer_SSL_20120508.keystore"
* @see 参数说明----->-genkey 表示生成密钥
* @see -alias 指定别名,这里是Jadyer_SSL_20120508
* @see -keyalg 指定算法,这里是RSA
* @see -validity 指定证书有效期,这里是1024天
* @see -keystore 指定存储位置,这里是D:\\Jadyer_SSL_20120508.keystore
* @see 2)CMD输出----->输入keystore密码:hongyu75
* @see 再次输入新密码:hongyu75
* @see 您的名字与姓氏是什么?[Unknown]:127.0.0.1(这里要根据实际情况填写网站域名或者IP,否则会出现证书上的名称无效)
* @see 您的组织单位名称是什么?[Unknown]:http://blog.csdn.net/jadyer
* @see 您的组织名称是什么?[Unknown]:JavaLover_jadyer
* @see 您所在的城市或区域名称是什么?[Unknown]:BJ
* @see 您所在的州或省份名称是什么?[Unknown]:BJ_NanTian
* @see 该单位的两字母国家代码是什么[Unknown]:CN
* @see CN=127.0.0.1, OU=http://blog.csdn.net/jadyer, O=JavaLover_jadyer, L=BJ, ST=BJ_NanTian, C=CN 正确吗?[否]:Y
* @see 输入<Jadyer_SSL_20120508>的主密码(如果和 keystore 密码相同,按回车):这里按回车键
* @see (这里的主密码一定要与keystore密码相同,否则启动Tomcat时就会告诉你java.io.IOException: Cannot recover key)
* @see 3)接下来就会按照-keystore参数值在指定位置生成指定的KeyStore文件了
* @see ===================================================================================================================================
* @see 2、让Tomcat支持SSL
* @see 1)将生成的Jadyer_SSL_20120508.keystore拷贝到\\%TOMCAT_HOME%\\conf\\目录中(其它目录也可以)
* @see 2)修改\\%TOMCAT_HOME%\\conf\\server.xml文件(大约在85行的位置),新增内容如下
* @see <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
* @see maxThreads="150" scheme="https" secure="true"
* @see clientAuth="false" sslProtocol="TLS" URIEncoding="UTF-8"
* @see keystoreFile="conf/Jadyer_SSL_20120508.keystore" keystorePass="hongyu75"/>
* @see 3)这样,我们的Tomcat就支持HTTPS访问了(关于<Connector/>标签中的属性说明,参拜Google大神)
* @see ===================================================================================================================================
* @see 3、用浏览器访问我们的应用
* @see 1)输入https://127.0.0.1:8443/blog会发现你的应用已经处于SSL安全通道中了
* @see 此时,如果我们在浏览器里访问http://127.0.0.1:8443/blog会发现,竟然能访问
* @see 也就是说,我们虽然启用了HTTPS,但现在还可以绕开HTTPS直接访问HTTP还能,这样HTTPS也就起不到作用了
* @see 2)我们可以配置一下\\%TOMCAT_HOME%\\conf\\web.xml文件,使得HTTP的访问能够重定向到HTTPS的连接
* @see 修改位置大约为web.xml的1224行,即在</welcome-file-list>标签后面加入下面的内容,即可
* @see <security-constraint>
* @see <!-- Authorization setting for SSL -->
* @see <web-resource-collection>
* @see <web-resource-name>SSL_App</web-resource-name>
* @see <!-- 指明需要SSL的url -->
* @see <url-pattern>/*</url-pattern>
* @see <http-method>GET</http-method>
* @see <http-method>POST</http-method>
* @see </web-resource-collection>
* @see <user-data-constraint>
* @see <!-- 指明需要SSL -->
* @see <transport-guarantee>CONFIDENTIAL</transport-guarantee>
* @see </user-data-constraint>
* @see </security-constraint>
* @see ===================================================================================================================================
* @author http://blog.csdn.net/jadyer
* @editor Feb 1, 2012 3:02:27 PM
*/
public class HttpClientUtil {
public static void main(String[] args)throws Exception{
//String requestUrl = "http://127.0.0.1:8088/test/web/userac";
String requestUrl = "https://127.0.0.1:8443/test/web/userac";
System.out.println(sendSSLRequest(requestUrl));
} /**
* 发送HTTPS请求
* @param requestUrl 请求的地址
* @return 响应内容
*/
@SuppressWarnings("finally")
public static String sendSSLRequest(String requestUrl){
long responseLength = 0; //响应长度
String responseContent = null; //响应内容
HttpClient httpClient = new DefaultHttpClient(); //创建默认的httpClient实例
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream fis = new FileInputStream(new File("F:\\Tool\\IDE\\Jadyer_SSL_20120508.keystore"));
try {
trustStore.load(fis, "hongyu75".toCharArray()); //加载KeyStore
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore); //创建Socket工厂,将trustStore注入
Scheme sch = new Scheme("https", 8443, socketFactory); //创建Scheme
httpClient.getConnectionManager().getSchemeRegistry().register(sch); //注册Scheme
HttpGet httpGet = new HttpGet(requestUrl); //创建HttpGet
HttpResponse response = httpClient.execute(httpGet); //执行GET请求
HttpEntity entity = response.getEntity(); //获取响应实体
if (null != entity) {
responseLength = entity.getContentLength();
responseContent = EntityUtils.toString(entity, "UTF-8");
EntityUtils.consume(entity); //Consume response content
}
System.out.println("请求地址: " + httpGet.getURI());
System.out.println("响应状态: " + response.getStatusLine());
System.out.println("响应长度: " + responseLength);
System.out.println("响应内容: " + responseContent);
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown(); //关闭连接,释放资源
return responseContent;
}
}
}

使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL的更多相关文章

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

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

  2. 通过 Apache Commons HttpClient 发送 HTTPS 请求

    1.通过 HTTPS 发送 POST 请求: 2.HTTPS 安全协议采用 TLSv1.2: 3. 使用代理(Proxy)进行 HTTPS 访问: 4.指定 Content-Type 为:applic ...

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

  4. python https请求报错:SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED]

    python爬虫,使用requests库发送https请求报错:SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 解决方法: imp ...

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

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

  6. Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  7. .net core使用HttpClient发送代理请求_程序内抓包_Fiddler抓包

    前言:  通过Fiddler抓取浏览器请求数据,相信大家已经都会用了,我们知道Fiddler是通过在本机计算器添加一个默认的代理服务器来实现的抓包数据的,端口号为:8888. 其实当我们打开Fiddl ...

  8. 【传输协议】发送https请求,由于客户端jdk版本过高,服务端版本低。导致异常:javax.net.ssl.SSLHandshakeException: Server chose SSLv3, but that protocol version is not enabled or not supported by the client.

    本地环境jdk为1.8,服务器使用jdk版本未知.但发送https请求,抛出如下异常,解决方案. 一:发送异常内容如下 javax.net.ssl.SSLHandshakeException: Ser ...

  9. .Net Core 发送https请求/.net core 调用数字证书 使用X509Certificate2

    .Net Core 发送https请求 .net core 调用数字证书 使用X509Certificate2 .NET下面的 .netfromwork使用和asp.net core下使用方式不一样 ...

随机推荐

  1. php中const与define的使用区别 详解

    1.const用于类成员变量定义,一旦定义且不能改变其值.define定义全局常量,在任何地方都可以访问. 2.define不能在类中定义而const可以. 3.const不能在条件语句中定义常量 i ...

  2. Entity Framework学习笔记(六)----使用Lambda查询Entity Framework(1)

    请注明转载地址:http://www.cnblogs.com/arhat 在前几章中,老魏一直使用Linq来查询Entity Framework.但是老魏感觉,如果使用Linq的话,那么Linq的返回 ...

  3. Oracle 存储过程实例2

    --创建存储过程 CREATE OR REPLACE PROCEDURE xxxxxxxxxxx_p ( --参数IN表示输入参数,OUT表示输入参数,类型可以使用任意Oracle中的合法类型. is ...

  4. OC的@property 和 @synthesize id

    学习java的JDBC,成员变量的setter和getter,eclipse都能帮我们自动生成:当然xcode这款编译器也很强大,也能自动生成: 1:@property @property是写在类的声 ...

  5. android 设置半透明

    对于Button和ImageButton 还有一些View 设置半透明或者透明都是通过 android:background="#b0000000" 这是就是半透明 android ...

  6. Xcode Provisioning 路径

    ~/Library/MobileDevice/Provisioning Profiles

  7. POJ 3614 Sunscreen 贪心

    题目链接: http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MSMemory Limit: 65536K 问题描述 to avoid ...

  8. BestCoder Round #3

    Task schedule http://acm.hdu.edu.cn/showproblem.php?pid=4907 #include<cstdio> #include<cstr ...

  9. 无法从 ajax.googleapis.com 下载问题

    除FQ外的解决办法: 打开目录 C:\Windows\System32\drivers\etc,修改 hosts 文件,添加一行 : 127.0.0.1 ajax.googleapis.com 打开I ...

  10. [nowCoder] 两个不等长数组求第K大数

    给定两个有序数组arr1和arr2,在给定一个整数k,返回两个数组的所有数中第K小的数.例如:arr1 = {1,2,3,4,5};arr2 = {3,4,5};K = 1;因为1为所有数中最小的,所 ...