使用https访问带有SOAP协议头用户验证的webservice接口

使用https访问带有SOAP协议头用户验证的webservice接口

1,添加所需jar包

https://download.csdn.net/download/weixin_42846140/13128960

2,相关代码

调用接口的方法,接口是参数是xml格式

/**
	 * 根据参数请求接口对于方法
	 * @param methodName 请求的方法名
	 * @param requestXml 请求参数
	 * @return
	 */
	public static String getExpDataList(String methodName,String requestXml) {
		/**
		此处通过配置文件读取serviceName 和namespace,可根据自己的方式修改,例:
		以下是一个对外开放的查询手机号的接口文档
		String serviceName = "https://jstqxkhz.jst-wl.cn:8092/WmsWebService/services/wmsservice?wsdl";
		String namespace = "http://webservice.fms.com";
		*/
		String serviceName = ReadPropertiesUtil.readWms("serviceName");
		String namespace = ReadPropertiesUtil.readWms("namespace");
		//通过https访问所需的证书## 标题文件,证书存放的路径也是通过配置文件读取的,可根据自己的方式修改
		System.setProperty("javax.net.ssl.trustStore",ReadPropertiesUtil.readWms("mykey"));
		Object[] methodArgs = new Object[] {requestXml};
		Class[] returnTypes = new Class[] { String.class };
		try {
			ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory(); 
			//默认443端口,可根据自己的方式修改 
			Protocol.registerProtocol("https", new Protocol("https", fcty, 443));  
			RPCServiceClient serviceClient = new RPCServiceClient();
			// 将创建的OMElement对象放置到Header中
			serviceClient.addHeader(HeaderOMElement.createHeaderOMElement());
			Options options = serviceClient.getOptions();
			EndpointReference targetEPR = new EndpointReference(serviceName);
			options.setTo(targetEPR);
			options.setManageSession(true);   
		    options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true); 
			QName op = new QName(namespace, methodName);
			Object[] response = serviceClient.invokeBlocking(op, methodArgs,returnTypes);
			String result = response[0].toString();
			if (result == null) {
				System.out.println("didn't initialize!");
				return null;
			} else {
				System.out.println("result ====== " + result);
				return result;
			}
		} catch (AxisFault e) {
			System.out.println("验证失败");
			e.printStackTrace();
			return null;
		}
	}

调用接口的方法用到的创建的OMElement对象方法:

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
//SOAP协议头用户验证
public class HeaderOMElement {
	public static OMElement createHeaderOMElement() {
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMNamespace SecurityElementNamespace = factory.createOMNamespace(
				"http://webservice.fms.com", "");
		OMElement authenticationOM = factory.createOMElement("Authentication",
				SecurityElementNamespace);
		OMElement usernameOM = factory.createOMElement("Username",
				SecurityElementNamespace);
		OMElement passwordOM = factory.createOMElement("Password",
				SecurityElementNamespace);
		//调用接口会进行身份验证,由服务器端提供客户身份信息(账户密码),根据自身情况修改身份信息
		usernameOM.setText("20201120");
		passwordOM.setText("20201120");
		authenticationOM.addChild(usernameOM);
		authenticationOM.addChild(passwordOM);
		return authenticationOM;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值