java调用webservice接口示例程序

走远了吗. 提交于 2020-02-18 17:36:47
package test;

import java.io.File;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.junit.Test;


public class Test {
	
	@Test
	public void test(){
		
		String url ="http://127.0.0.1:7001/services/XXWebService?wsdl";
		RPCServiceClient serviceClient;
		
		
		try {
			serviceClient=new RPCServiceClient();
			Options options = serviceClient.getOptions();
			EndpointReference targetEPR = new EndpointReference(url);
			options.setTransportInProtocol("SOAP");
			options.setAction("doService");
			options.setTo(targetEPR);
			options.setManageSession(true);
			options.setProperty(HTTPConstants.CHUNKED,"false");
			options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, true);
			options.setTimeOutInMilliSeconds(60000);
			
			QName opAddEntry = new QName("http://impl.service.xxx","doService");//服务端包名的反序
			
			String xml = createXml();
			
			Object[] objects=new Object[]{xml};
			Class[] classes=new Class[]{String.class};
			
			Object obj = serviceClient.invokeBlocking(opAddEntry, objects,classes)[0];
			System.out.println(obj);
			
			serviceClient.cleanupTransport();
			serviceClient.cleanup();
			
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private String createXml() {
		// TODO Auto-generated method stub
		SAXReader saxReader = new SAXReader();
		Document doc=null;
		
		try {
			doc= saxReader.read(new File("E:\\request.xml"));
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return doc.asXML();
	}

}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!