【备忘】Java调用Webservice实例

环境:JDK1.5(公司平台限制,木有法子)

有待补充的是将OMElement转换成List。

下面是用到的jar,比较全了,实验从网上找到的代码,基本的错误都是缺少jar。

XmlSchema-1.4.7.jar
axiom-api-1.2.13.jar
axiom-impl-1.2.13.jar
axis2-adb-1.6.2.jar
axis2-kernel-1.6.2.jar
axis2-transport-http-1.6.2.jar
axis2-transport-local-1.6.2.jar
commons-codec-1.3.jar
commons-io-1.4.jar
commons-logging-1.1.1.jar
httpcore-4.0.jar
mail-1.4.jar
neethi-3.0.2.jar
wsdl4j-1.6.2.jar
stax-api-1.0.1.jar
wstx-asl-3.2.0.jar
commons-httpclient-3.1.jar

package com.pi3000.pz;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

public class ServiceGetWeather {
	private static EndpointReference targetEPR = new EndpointReference(
    "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
	
	
	public void getResult()throws Exception{
		System.out.println("getResult");
		ServiceClient sender = new ServiceClient();
		sender.setOptions(buildOptions());
		OMElement result = sender.sendReceive(buildParam());
	}
	private static OMElement buildParam() {
		System.out.println("buildParam");
		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "");
		OMElement data = fac.createOMElement("getWeatherbyCityName", omNs);
		OMElement inner = fac.createOMElement("theCityName", omNs);
		inner.setText("北京");
		data.addChild(inner);
		return data;
	}
	
	private static Options buildOptions() {
		Options options = new Options();
		options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
		options.setAction("http://WebXml.com.cn/getWeatherbyCityName");
		options.setTo(targetEPR);
		//options.setProperty(propertyKey, property)
		
		// enabling MTOM in the client side
		// options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
		options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
		return options;
	}
	public static void main(String[] args) throws Exception {
		ServiceGetWeather s = new ServiceGetWeather();
		s.getResult();
	}

}


你可能感兴趣的:(【备忘】Java调用Webservice实例)