加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Axis2调用webService服务(调用而非发布)

发布时间:2020-12-16 23:17:38 所属栏目:安全 来源:网络整理
导读:package com.shu.test;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.SOAP11Constants;import org.apac
package com.shu.test;

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.SOAP11Constants;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;

public class CopyOfServiceTest {
	
	private static EndpointReference targetEPR =new EndpointReference("http://webservice.webxml.com.cn/WebServices/TranslatorWebService.asmx");
	private static OMFactory fac = OMAbstractFactory.getOMFactory();
	static OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/","tns");
	
	public static void main(String[] args) throws AxisFault {
	     ServiceClient sender = new ServiceClient();
	     sender.setOptions(buildOptions("http://WebXml.com.cn/getEnCnTwoWayTranslator"));
	     OMElement result = sender.sendReceive(buildParam("getEnCnTwoWayTranslator",new String[]{"Word"},new String[]{"loser"}));
	     System.out.println(result);
	}
	
	public static  OMElement buildParam(String method,String[] arg,String[] val) {
        OMElement data = fac.createOMElement(method,omNs);
        for(int i=0;i<arg.length;i++){
	        OMElement inner = fac.createOMElement(arg[i],omNs);
	        inner.setText(val[i]);
	        data.addChild(inner);
        }
        return data;

	}
	
	public static Options  buildOptions(String action){
		Options options = new Options();
		options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
		options.setTo(targetEPR);
		options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
		options.setProperty(HTTPConstants.CHUNKED,"false");//设置不受限制.
		options.setProperty(Constants.Configuration.HTTP_METHOD,HTTPConstants.HTTP_METHOD_POST);
		options.setAction(action);
		return options; 
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读