版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Java 发送 https 的 post 请求方法import java.io.BufferedReader;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import .MalformedURLException;import .URL;import java.security.GeneralSecurityException;import java.security.KeyStore;import .ssl.HostnameVerifier;import
2、.ssl.HttpsURLConnection;import .ssl.KeyManagerFactory;import .ssl.SSLContext;import .ssl.TrustManagerFactory;public class HttpsPost /* 获得 KeyStore.* param keyStorePath密钥库路径* param password* 密码* return 密钥库* throws Exception*/public static KeyStore getKeyStore(String password,String keyStorePath)throw
3、s Exception / 实例化密钥库KeyStore ks = KeyStore.getInstance(JKS);/ 获得密钥库文件流FileInputStream is = newFileInputStream(keyStorePath);/ 加载密钥库ks.load(is, password.toCharArray();/ 关闭密钥库文件流is.close();return ks;/*获得 SSLSocketFactory.param password密码* param keyStorePath*密钥库路径* param trustStorePath*信任库路径* return SS
4、LSocketFactory* throws Exception*/public static SSLContext getSSLContext(String password,String keyStorePath, String trustStorePath) throws Exception / 实例化密钥库KeyManagerFactory keyManagerFactory =KeyManagerFactory .getInstance(KeyManagerFactory.getDe faultAlgorithm();/ 获得密钥库KeyStore keyStore = getKey
5、Store(password, keyStorePath);/ 初始化密钥工厂keyManagerFactory.init(keyStore,password.toCharArray();/ 实例化信任库TrustManagerFactory trustManagerFactory =TrustManagerFactory .getInstance(TrustManagerFactory.getD efaultAlgorithm();/ 获得信任库KeyStore trustStore = getKeyStore(password, trustStorePath);/ 初始化信任库 trust
6、ManagerFactory.init(trustStore);/ 实例化 SSL 上下文 SSLContext ctx = SSLContext.getInstance(TLS);/ 初始化 SSL 上下文ctx.init(keyManagerFactory.getKeyManagers(),trustManagerFactory.getTrustManagers(), null);/ 获得 SSLSocketFactory return ctx;* 初始化 HttpsURLConnection.* param password* 密码* param keyStorePath*密钥库路径*
7、param trustStorePath*信任库路径* throws Exception*/public static void initHttpsURLConnection(String password,String keyStorePath, String trustStorePath) throws Exception / 声明 SSL 上下文SSLContext sslContext = null;/ 实例化主机名验证接口HostnameVerifier hnv = new MyHostnameVerifier();try sslContext = getSSLContext(pas
8、sword,keyStorePath, trustStorePath); catch (GeneralSecurityException e) e.printStackTrace();if (sslContext != null) HttpsURLConnection.setDefaultSSLSocketFactory(sslCon text.getSocketFactory();HttpsURLConnection.setDefaultHostnameVerifier(hnv);发送请求 .param httpsUrl请求的地址param xmlStr请求的数据*/public stati
9、c void post(String httpsUrl, String xmlStr) HttpsURLConnection urlCon = null;try urlCon = (HttpsURLConnection) (newURL(httpsUrl).openConnection();urlCon.setDoInput(true);urlCon.setDoOutput(true);urlCon.setRequestMethod(POST);urlCon.setRequestProperty(Content-Length,String.valueOf(xmlStr.getBytes().l
10、ength);urlCon.setUseCaches(false);/设置为 gbk 可以解决服务器接收时读取的数 据中文乱码问题urlCon.getOutputStream().write(xmlStr.getBytes(gbk);urlCon.getOutputStream().flush();urlCon.getOutputStream().close();BufferedReader in = newBufferedReader(new InputStreamReader(urlCon.getInputStream();String line;while (line = in.read
11、Line() != null) System.out.println(line); catch (MalformedURLException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); catch (Exception e) e.printStackTrace();* 测试方法 .* param args* throws Exception*/public static void main(String args) throws Exception / 密码String password = 123456
12、;/ 密钥库String keyStorePath = tomcat.keystore;/ 信任库String trustStorePath = tomcat.keystore;/ 本地起的 https 服务String httpsUrl = https:/localhost:8443/service/httpsPost;/ 传输文本String xmlStr = <?xml version=1.0 encoding=UTF-8?><fruitShop><fruits><fr uit><kind> 萝卜 </kind>&l
13、t;/fruit><fruit><kind> 菠萝 </kind></fruit></fruits></fruitShop>HttpsPost.initHttpsURLConnection(password, keyStorePath, trustStorePath);/ 发起请求HttpsPost.post(httpsUrl, xmlStr); java viewplaincopyimport .ssl.HostnameVerifier;import .ssl.SSLSession;实现用于主机名验证的基接口。在握
14、手期间, 如果 URL 的主机名和服务器的标识主机名 不匹配,则验证机制可以回调此接口的实现程序来确定是否 应该允许此连接。*/public class MyHostnameVerifier implements HostnameVerifier Overridepublic boolean verify(String hostname, SSLSession session) if(localhost.equals(hostname) return true; else return false; 接收请求的 Web 应用:web.xmlhtml view plaincopy<?xml
15、 version=1.0 encoding=UTF-8?> <web-app version=2.5xmlns=xmlns:xsi=/2001/XMLSchema-instancexsi:schemaLocation=<servlet><servlet-name>rollBack</servlet-name><servlet-class>rollBack</servlet-class></servlet><servlet-mapping><servlet-nam
16、e>rollBack</servlet-name><url-pattern>/httpsPost</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>rollBack servletjava viewplaincopyimport java.io.BufferedReader;import java.
17、io.IOException;import java.io.InputStreamReader;import javax.servlet.ServletException;import javax.servlet.ServletInputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class rollBack extends HttpServlet public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException /获取请求流ServletInputStream sis =request.getInputStream();BufferedRea
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 印泥制作工岗位适应考核试卷含答案
- 自来水笔制造工管理应用强化考核试卷含答案
- 丁苯橡胶装置操作工岗前实践水平考核试卷含答案
- 分蜜机工岗中生产安全效果考核试卷含答案
- 流体装卸操作工持续改进模拟考核试卷含答案
- 橡胶硫化工风险评估水平考核试卷含答案
- 锅炉清洗工岗位制度考核试卷含答案
- 初三【数学(北京版)】直线和圆的位置关系 教学设计
- 2026年城市规划与生态修复工程师试题及答案
- 2026年食品营养设计食品营养师资格认证考试试题及答案
- 旋挖成孔灌注桩施工监理实施细则
- GB/T 45021.1-2024光伏组件性能测试和能量评定第1部分:辐照度和温度性能测量和功率评定
- GB/T 7991.10-2024搪玻璃层试验方法第10部分:生产和贮存食品的搪玻璃设备搪玻璃层中重金属离子溶出量的测定和限值
- 完整版:美制螺纹尺寸对照表(牙数、牙高、螺距、小径、中径外径、钻孔)
- 柏拉图法则分析课件
- 汽轮机DEH简介和SGC顺控启动
- 治疗肝癌的中药药方17则
- 《普通心理学》彭聃龄版解析
- JJF 1449-2014崩解时限测试仪校准规范
- GB/T 30227-2013图书馆古籍书库基本要求
- GA/T 1255-2016警用数字集群(PDT)通信系统射频设备技术要求和测试方法
评论
0/150
提交评论