加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

基于https,HttpsURLConnection客户端代码调用

发布时间:2020-12-16 23:54:25 所属栏目:安全 来源:网络整理
导读:有关tomcat 6.0如何配置https服务的文章可以参考:http://www.voidcn.com/article/p-mstxlhct-ex.html 以下主要讲解如何使用https发起post请求: 参考文档:梁栋前辈的《Java加密与解密的艺术》 [java] ? view plain copy import ?java.io.BufferedReader;??

有关tomcat 6.0如何配置https服务的文章可以参考:http://www.voidcn.com/article/p-mstxlhct-ex.html

以下主要讲解如何使用https发起post请求:

参考文档:梁栋前辈的《Java加密与解密的艺术》

[java]? view plain copy
  1. import?java.io.BufferedReader;??
  2. import?java.io.FileInputStream;??
  3. import?java.io.IOException;??
  4. import?java.io.InputStreamReader;??
  5. import?java.net.MalformedURLException;??
  6. import?java.net.URL;??
  7. import?java.security.GeneralSecurityException;??
  8. import?java.security.KeyStore;??
  9. ??
  10. import?javax.net.ssl.HostnameVerifier;??
  11. import?javax.net.ssl.HttpsURLConnection;??
  12. import?javax.net.ssl.KeyManagerFactory;??
  13. import?javax.net.ssl.SSLContext;??
  14. import?javax.net.ssl.TrustManagerFactory;??
  15. public?class?HttpsPost?{??
  16. ????/**?
  17. ?????*?获得KeyStore.?
  18. ?????*?@param?keyStorePath?
  19. ?????*????????????密钥库路径?
  20. ?????*?@param?password?
  21. ?????*????????????密码?
  22. ?????*?@return?密钥库?
  23. ?????*?@throws?Exception?
  24. ?????*/??
  25. ????static?KeyStore?getKeyStore(String?password,?String?keyStorePath)??
  26. ????????????throws?Exception?{??
  27. ????????//?实例化密钥库??
  28. ????????KeyStore?ks?=?KeyStore.getInstance("JKS");??
  29. //?获得密钥库文件流??
  30. ????????FileInputStream?is?=?new?FileInputStream(keyStorePath);??
  31. //?加载密钥库??
  32. ????????ks.load(is,?password.toCharArray());??
  33. //?关闭密钥库文件流??
  34. ????????is.close();??
  35. ????????return?ks;??
  36. ????}??
  37. ??
  38. ?????*?获得SSLSocketFactory.?
  39. ?????*?@param?trustStorePath?
  40. ?????*????????????信任库路径?
  41. ?????*?@return?SSLSocketFactory?
  42. static?SSLContext?getSSLContext(String?password,??
  43. ????????????String?keyStorePath,?String?trustStorePath)? ????????KeyManagerFactory?keyManagerFactory?=?KeyManagerFactory??
  44. ????????????????.getInstance(KeyManagerFactory.getDefaultAlgorithm());??
  45. ????????//?获得密钥库??
  46. ????????KeyStore?keyStore?=?getKeyStore(password,?keyStorePath);??
  47. //?初始化密钥工厂??
  48. ????????keyManagerFactory.init(keyStore,?password.toCharArray());??
  49. //?实例化信任库??
  50. ????????TrustManagerFactory?trustManagerFactory?=?TrustManagerFactory??
  51. ????????????????.getInstance(TrustManagerFactory.getDefaultAlgorithm());??
  52. //?获得信任库??
  53. ????????KeyStore?trustStore?=?getKeyStore(password,?trustStorePath);??
  54. //?初始化信任库??
  55. ????????trustManagerFactory.init(trustStore);??
  56. //?实例化SSL上下文??
  57. ????????SSLContext?ctx?=?SSLContext.getInstance("TLS");??
  58. //?初始化SSL上下文??
  59. ????????ctx.init(keyManagerFactory.getKeyManagers(),??
  60. ????????????????trustManagerFactory.getTrustManagers(),?null);??
  61. //?获得SSLSocketFactory??
  62. ????????return?ctx;??
  63. ????}??
  64. ????/**?
  65. ?????*?初始化HttpsURLConnection.?
  66. ?????*?@param?password?
  67. ?????*????????????密码?
  68. ?????*?@param?keyStorePath?
  69. ?????*????????????密钥库路径?
  70. ?????*?@param?trustStorePath?
  71. ?????*????????????信任库路径?
  72. static?void?initHttpsURLConnection(String?password,0); background-color:inherit">//?声明SSL上下文??
  73. ????????SSLContext?sslContext?=?null;??
  74. //?实例化主机名验证接口??
  75. ????????HostnameVerifier?hnv?=?new?MyHostnameVerifier();??
  76. try?{??
  77. ????????????sslContext?=?getSSLContext(password,?keyStorePath,?trustStorePath);??
  78. ????????}?catch?(GeneralSecurityException?e)?{??
  79. ????????????e.printStackTrace();??
  80. ????????}??
  81. if?(sslContext?!=?null)?{??
  82. ????????????HttpsURLConnection.setDefaultSSLSocketFactory(sslContext??
  83. ????????????????????.getSocketFactory());??
  84. ????????HttpsURLConnection.setDefaultHostnameVerifier(hnv);??
  85. ?????*?发送请求.?
  86. ?????*?@param?httpsUrl?
  87. ?????*????????????请求的地址?
  88. ?????*?@param?xmlStr?
  89. ?????*????????????请求的数据?
  90. ?????*/??
  91. ????void?post(String?httpsUrl,?String?xmlStr)?{??
  92. ????????HttpsURLConnection?urlCon?=?null;??
  93. try?{??
  94. ????????????urlCon?=?(HttpsURLConnection)?(new?URL(httpsUrl)).openConnection();??
  95. ????????????urlCon.setDoInput(true);??
  96. ????????????urlCon.setDoOutput(true);??
  97. ????????????urlCon.setRequestMethod("POST");??
  98. ????????????urlCon.setRequestProperty("Content-Length",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????????????????????String.valueOf(xmlStr.getBytes().length));??
  99. ????????????urlCon.setUseCaches(false);??
  100. ????????????//设置为gbk可以解决服务器接收时读取的数据中文乱码问题??
  101. ????????????urlCon.getOutputStream().write(xmlStr.getBytes("gbk"));??
  102. ????????????urlCon.getOutputStream().flush();??
  103. ????????????urlCon.getOutputStream().close();??
  104. ????????????BufferedReader?in?=?new?BufferedReader(new?InputStreamReader(??
  105. ????????????????????urlCon.getInputStream()));??
  106. ????????????String?line;??
  107. ????????????while?((line?=?in.readLine())?!=?null)?{??
  108. ????????????????System.out.println(line);??
  109. ????????????}??
  110. ????????}?catch?(MalformedURLException?e)?{??
  111. ????????????e.printStackTrace();??
  112. catch?(IOException?e)?{??
  113. catch?(Exception?e)?{??
  114. ????????}??
  115. ?????*?测试方法.?
  116. ?????*?@param?args?
  117. ?????*?@throws?Exception?
  118. void?main(String[]?args)?//?密码??
  119. ????????String?password?=?"123456";??
  120. //?密钥库??
  121. ????????String?keyStorePath?=?"tomcat.keystore";??
  122. //?信任库??
  123. ????????String?trustStorePath?=?"tomcat.keystore";??
  124. //?本地起的https服务??
  125. ????????String?httpsUrl?=?"https://localhost:8443/service/httpsPost";??
  126. //?传输文本??
  127. ????????String?xmlStr?=?"<?xml?version="1.0"?encoding="UTF-8"?><fruitShop><fruits><fruit><kind>萝卜</kind></fruit><fruit><kind>菠萝</kind></fruit></fruits></fruitShop>";??
  128. ????????HttpsPost.initHttpsURLConnection(password,0); background-color:inherit">//?发起请求??
  129. ????????HttpsPost.post(httpsUrl,?xmlStr);??
  130. }??

copy
    import?javax.net.ssl.HostnameVerifier;??
  1. import?javax.net.ssl.SSLSession;??
  2. ?*?实现用于主机名验证的基接口。??
  3. ?*?在握手期间,如果?URL?的主机名和服务器的标识主机名不匹配,则验证机制可以回调此接口的实现程序来确定是否应该允许此连接。?
  4. ?*/??
  5. class?MyHostnameVerifier?implements?HostnameVerifier?{??
  6. ????@Override??
  7. boolean?verify(String?hostname,?SSLSession?session)?{??
  8. if("localhost".equals(hostname)){??
  9. return?true;??
  10. else?{??
  11. false;??
  12. }??

接收请求的Web应用:

web.xml

[html]? copy
    <?xml?version="1.0"?encoding="UTF-8"?>??
  1. <web-app?version="2.5"???
  2. ????xmlns="http://java.sun.com/xml/ns/javaee"???
  3. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"???
  4. ????xsi:schemaLocation="http://java.sun.com/xml/ns/javaee???
  5. ????http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">??
  6. ??servlet>??
  7. ????servlet-name>rollBack</ ????servlet-class ??servlet-mappingurl-pattern>/httpsPostwelcome-file-listwelcome-file>index.jspweb-app>??

rollBack servlet

copy
    import?java.io.IOException;??
  1. import?java.io.InputStreamReader;??
  2. import?javax.servlet.ServletException;??
  3. import?javax.servlet.ServletInputStream;??
  4. import?javax.servlet.http.HttpServlet;??
  5. import?javax.servlet.http.HttpServletRequest;??
  6. import?javax.servlet.http.HttpServletResponse;??
  7. class?rollBack?extends?HttpServlet?{??
  8. void?doGet(HttpServletRequest?request,?HttpServletResponse?response)??
  9. throws?ServletException,?IOException?{??
  10. //获取请求流??
  11. ????????ServletInputStream?sis?=?request.getInputStream();??
  12. ????????BufferedReader?in?=?new?InputStreamReader(sis));??
  13. ????????String?line;??
  14. if((line?=?in.readLine())?!=?null){??
  15. ????????????System.out.println(line);??
  16. ????????in.close();??
  17. void?doPost(HttpServletRequest?request,153); background-color:inherit; font-weight:bold">this.doGet(request,?response);??
  18. }??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读