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

时间:2021-06-10 10:24:42

一、创建证书

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();
}
}
}