博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java发送https请求证书问题
阅读量:4219 次
发布时间:2019-05-26

本文共 2670 字,大约阅读时间需要 8 分钟。

配证书:
Getting Tomcat SSL (https) Working
1.Create a certificate keystore containing a single self-signed certificate by executing the following command. Specify a password value of "changeit ". Note that this command creates both the certificate and the keystore
Windows: 进到%JAVA_HOME%\bin\目录下输入:
keytool -genkey -alias tomcat -keyalg RSA -keystore tomcat.keystore
产生一个tomcat.keystore 文件
Unix: $JAVA_HOME/bin/ 输入: keytool -genkey -alias tomcat -keyalg RSA -keystore tomcat.keystore
到D:\Tomcat 5.0\conf目录下输入: keytool -genkey -alias tomcat -keyalg RSA -keystore tomcat.keystore
输入keystore密码: changeit
您的名字与姓氏是什么?
[Unknown]: localhost
您的组织单位名称是什么?
[Unknown]: huawei
您的组织名称是什么?
[Unknown]: hell
您所在的城市或区域名称是什么?
[Unknown]: hangzhou
您所在的州或省份名称是什么?
[Unknown]: zhejiang
该单位的两字母国家代码是什么
[Unknown]: ch
CN=localhost, OU=wict, O=hell, L=wuhan, ST=hubei, C=ch 正确吗?
[否]: y
输入 <tomcat> 的主密码
(如果和 keystore 密码相同,按回车): 这里我按了回车
2. Copy the keystore file to CATALINA_HOME/conf 拷贝到tomcat的conf下
3. Uncomment the "SSL HTTP/1.1 Connector " entry in $CATALINA_HOME/conf/server.xml. Your entry should look like:
<!-- Define a SSL Coyote HTTP/1.1 Connector on port 8443 -->
<!---->
<Connector port="8443" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" disableUploadTimeout="true"
acceptCount="100" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile= "/conf/tomcat.keystore "
truststoreFile= "/conf/tomcat.keystore "/>
添加后保存,浏览其中输入以下地址访问tomcat首页 https://localhost:8443
4. Copy the keystore file to the default file location of the Java Applications, then run the Java Application。 拷贝到工程目录下,注意,直接再工程这级目录下面。
5、java代码:
public static void main(String[] args) throws Exception {
//serverkeys是通过keytool生成的自己的证书 System.setProperty( "javax.net.ssl.trustStore", "tomcat.keystore"); System.setProperty( "javax.net.ssl.trustStorePassword", "changeit"); //connect to https https://www.sun.com URL url = new URL( "https://localhost:8443"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod( "POST"); connection.setDoOutput(true); connection.setDoInput(true); System.out.println( "1--Conected to "+ connection.toString()); DataOutputStream(connection.getOutputStream()); StringBuffer outbuff = new StringBuffer(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { outbuff.append(line); } in.close(); System.out.println( "3---Test : " + outbuff.toString()); }

转载地址:http://dgqmi.baihongyu.com/

你可能感兴趣的文章
游戏感:虚拟感觉的游戏设计师指南——第十八章 我想做的游戏
查看>>
游戏设计的艺术:一本透镜的书——第十章 某些元素是游戏机制
查看>>
游戏设计的艺术:一本透镜的书——第十一章 游戏机制必须平衡
查看>>
游戏设计的艺术:一本透镜的书——第十二章 游戏机制支撑谜题
查看>>
游戏设计的艺术:一本透镜的书——第十三章 玩家通过界面玩游戏
查看>>
编写苹果游戏中心应用程序(翻译 1.3 为iOS应用程序设置游戏中心)
查看>>
编写苹果游戏中心应用程序(翻译 1.4 添加游戏工具包框架)
查看>>
编写苹果游戏中心应用程序(翻译 1.5 在游戏中心验证本地玩家)
查看>>
编写苹果游戏中心应用程序(翻译 1.6 获取本地玩家的信息)
查看>>
编写苹果游戏中心应用程序(翻译 1.7 在游戏中心添加朋友)
查看>>
编写苹果游戏中心应用程序(翻译 1.8 获取本地玩家的好友信息)
查看>>
WebGL自学教程《OpenGL ES 2.0编程指南》翻译——勘误表
查看>>
WebGL自学教程——WebGL示例:12. 要有光
查看>>
WebGL自学教程——WebGL示例:13.0 代码整理
查看>>
WebGL自学教程——WebGL示例:14.0 代码整理
查看>>
恶心的社会
查看>>
中国式危机公关9加1策略(第五章 慎用信息控制策略)
查看>>
展现自己的人生智慧
查看>>
深入理解java多态性
查看>>
Java新手进阶:细说引用类型
查看>>