微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐