axis2 调用.net基于https的WebService接口

本文介绍如何使用Java调用HTTPS WebService,并通过IE下载证书解决信任问题。具体步骤包括创建证书、配置信任库文件及Java代码实现。此外,还提供了一个具体的调用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、创建证书

1,在IE中访问WSDL的URL,弹出“安全警报”窗口,查看证书->详细信息标签页->复制到文件->下一步->下一步->指定文件名,将证书下载保存为.cer文件,例如:test_axis.cer

2,用下载到的证书文件生成信任库文件:

>keytool -import -file test_axis.cer -storepass changeit -keystore client.truststore -alias serverkey -noprompt

3,在调用WebService代码前指定信任库文件的路径:

System.setProperty("javax.net.ssl.trustStore", "/tmp/client.truststore");

System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

然后在java调用代码中加入

二、调用执行

package kind.util;

import java.util.Iterator;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

public class WebServiceUtil {

    private static EndpointReference targetEPR = new EndpointReference(
            "https://XXXXXX/IXXX.asmx?wsdl");// 接口WebService地址
    
    public static void main(String[] args) {
        try {

            System.setProperty("javax.net.ssl.trustStore", "D://client.truststore");  
            System.setProperty("javax.net.ssl.trustStorePassword", "changeit");  
            
            OMFactory fac = OMAbstractFactory.getOMFactory();

            OMNamespace omNs = fac.createOMNamespace(
                    "http://tempuri.org/", "tns");// 命名空间
 
            // 请求参数设置
            Options options = new Options();
            options.setTo(targetEPR);// 设定webservice地址
            options.setTransportInProtocol(Constants.TRANSPORT_HTTP);// 设定传输协议
            options.setSoapVersionURI(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);// 设定SOAP版本soap1.2

            // 客户端绑定参数设置
            ServiceClient sender = new ServiceClient();
            sender.setOptions(options);

            // 设定访问的接口方法

            OMElement method = fac.createOMElement("Authorization", omNs);// 要调用的接口方法名称
            
            OMElement value1 = fac.createOMElement("username", omNs);// 方法的第一个参数名称
            value1.addChild(fac.createOMText(value1, "username"));// 设定参数的值
            method.addChild(value1);// 方法设置参数
            
            OMElement value2 = fac.createOMElement("password", omNs);// 方法的第一个参数名称
            value2.addChild(fac.createOMText(value2, "password"));// 设定参数的值
            method.addChild(value2);// 方法设置参数
 
            OMElement result = sender.sendReceive(method);// 调用接口方法
            Iterator iterator = result.getChildrenWithLocalName("AuthorizationResult");
            System.out.println("guid="+((OMElement)iterator.next()).getText());
        }

        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

转载于:https://www.cnblogs.com/101key/p/3711544.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值