一,java使用 keytool工具创建证书
CMD窗口中运行 "keytool -genkey -alias "别名" -keyalg RSA -validity "有效时间" -keystore "生成证书路径"
二,配制tomcat支持ssl
在tomcat的config server.xml中打开 Connector结点
<Connector port="443" protocol="org.apache.coyote.http11.Http11Protocol" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
keystoreFile="yourPath/xx.keystore" keystorePass="yourPwd"
clientAuth="false" sslProtocol="TLS" />
这里注意下,在tomcat6下
protocol="HTTP/1.1 "
<!-- 改为 -->
protocol="org.apache.coyote.http11.Http11Protocol"
否则起动会报错
三,工程中web.xml配制
上两部己实现了ssl的访问 https://localst (443为https默认端口所以不用加端口号)就己是ssl安全通道中,但是我们还是可以通过http://localhost:port 访问,这里要让它http时自动成https
在web.xml中加入下面内容
<security-constraint>
<web-resource-collection>
<web-resource-name>SSL</web-resource-name>
<url-pattern>/xxx/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
在url-pattern中配制的是你要拦截的请求路径,比如只想对login模块强制转成https,则可配 /login/*
四,此时让tomcat支持https己全部配制完成,浏览器第一次访问时因为是自己制作的证书可以会有安全信任问题,要添加例外。想要httpclient发送https的请求,则还需如下的步骤
五,从浏览器中导出让才的证书(crt,pem后缀都可以)然后用keytool工具导入keystore,命令如下:
keytool -import -alias "localhost" -file xxx.crt -keystore ooo.keystore
xxx.crt : 刚从浏览器导出的证书
ooo.keystore :命令生成的keystore文件
六,java代码 (不得不说httpclient3和4版本在使用上差异还很大,用习惯了3总感觉4并不太顺手)
public static String sendSSLRequest(String requestUrl) {
long responseLength = 0; // 响应长度
String responseContent = null; // 响应内容
HttpClient httpClient = new DefaultHttpClient(); // 创建默认的httpClient实例
KeyStore trustStore = null;
InputStream fis = null;
HttpGet httpGet = null;
HttpResponse response = null;
try {
trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
fis = HttpsBaseAction.class.getClassLoader().getResourceAsStream(
"my.keystore");
trustStore.load(fis, "pwd".toCharArray()); // 加载KeyStore
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore); // 创建Socket工厂,将trustStore注入
Scheme sch = new Scheme("https", 443, socketFactory); // 创建Scheme
httpClient.getConnectionManager().getSchemeRegistry().register(sch); // 注册Scheme
httpGet = new HttpGet(requestUrl); // 创建HttpGet
Header headers[] = { new BasicHeader("myheader", "value") };
httpGet.setHeaders(headers); // 设置头信息
// 设置请求参数,get好像只能在url那里用?xx=xx传参(暂时没找到到别的可传参方法)
// 如用post传参如下
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("prameName", "value"));
UrlEncodedFormEntity uefEntity;
uefEntity = new UrlEncodedFormEntity(formparams, HTTP.UTF_8);
HttpPost post = new HttpPost(requestUrl);
post.setEntity(uefEntity);
post.setHeader("myheader", "value");
// response = httpClient.execute(post); // 执行POST请求
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 (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} finally {
httpClient.getConnectionManager().shutdown(); // 关闭连接,释放资源
return responseContent;
}
}
}
分享到:
相关推荐
不会的可以在评论区留言哈,这是我自己做项目用到的。所以绝对可用!同时共享出来给到大家
解决证书错误,利用httpClient发送https请求,内容格式为XML,另外包中有sha1加密,需要的包,速速下载
通过HTTPClient发起HTTPS GET请求(返回值为字符串)、POST请求(返回值为JSONOBJECT),需引入fastJson的jar包
esp8266 发起https 请求,请求 https 的api 等等,整个项目工程
httpclient发送post请求
NULL 博文链接:https://daoshud1.iteye.com/blog/1847378
NULL 博文链接:https://cxl2012.iteye.com/blog/2210521
HttpClientUtil工具类发送get和post请求,支持http和https,支持发送文件
NULL 博文链接:https://javasam.iteye.com/blog/2117845
NULL 博文链接:https://bijian1013.iteye.com/blog/2299764
本篇文章主要介绍了JAVA利用HttpClient进行HTTPS接口调用的方法,具有一定的参考价值,有兴趣的可以了解一下
NULL 博文链接:https://bijian1013.iteye.com/blog/2310211
httplient向https发送请求会因为证书校验而报错,该工具类提供绕开HTTPS证书校验方法,以实现访问https网站的功能
声明式HTTP客户端API框架,让Java发送HTTP/HTTPS请求不再难。它比OkHttp和HttpClient更高层,是封装调用第三方restful api client接口的好帮手,是retrofit和feign之外另一个选择。通过在接口上声明注解的方式配置...
该实例是以springBoot框架,jdk8、maven,包含HttpClient工具类,一般请求。https请求加载证书工具类。并附有测试类。
本方法以HttpClient发送请求,并且接收返回数据 举例说明 public static String doGet(String url, Map, String> params, String charset) public static void downLoadImage(String url, String path) public ...
使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。 1. 创建HttpClient对象。 2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost...