java使用cxf调用https方式的webservice

第一步:入cxf必须的jar包,当前使用的cxf的版本为3.0.1

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-ws-security</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.wss4j</groupId>
            <artifactId>wss4j-ws-security-dom</artifactId>
            <version>2.0.1</version>
        </dependency>

第二步:导出网站证书

https区别于http即https需要证书,接下来我们导出证书。
以下操作方式为谷歌内核浏览器操作方式,各大浏览器均支持导出证书,具体不了解的自行百度。
浏览器输入接口地址https://xxxxx/abc/ws/soap/sendHis?wsdl
在接口地址上f12进入开发者模式
点击sercurty选择 view certificate

保存证书
选择详细信息。点击保存证书

格式选择二进制
格式选择二进制,到此我们得到一个.cer文件-这个就是客户端的证书

第三步:生成key

确保本地安装了jdk,我们需要使用jdk中的keytool工具。
在cmd命令行,输入

>keytool -import -alias nciic -file d:\zs.cer -keystore d:\nciic.keystore

生成key,生成过程中会需要你输入key的密码,密码记录下来这个到后面会有用。

第四步:调用

        System.setProperty("javax.net.ssl.trustStore","d://nciic.keystore"); //key路径
        System.setProperty("javax.net.ssl.trustStorePassword","your pwd");//密码
        System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol"); 
        java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 

        DynamicClientFactory factory = DynamicClientFactory.newInstance();
        Client client = factory.createClient("https://xxxxx/abc/ws/soap/sendHis?wsdl");

其实很简单,就是在我们实际调用之前,添加了4行代码

Object[] results = client.invoke("login","your json");//调用方法

至此,使用Java调用Https方式的接口调用成功。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值