天气预报:
endpoint: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
wsdl: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
方法1:用axis2\bin\wsdl2java生成stub。
1、到http://ws.apache.org/axis2下载axis-binary.zip
2、设置Axis2的Home环境:
变量名:AXIS2_HOME
值:D:\axis2\axis2-1.4.1-bin\axis2-1.4.1 就是你下下来的安装目录。
3、进入命令行:出入 %AXIS2_HOME%\bin\wsdl2java -uri http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl -p cn.com.webxml -s -o stub
参数:uri ---你要生成JAVA代码需要的wsdl文件,可以是网上的,也可以是本地的。
-p 生成出的stub的包的生命 -o创建出的文件夹名。
完成后会在相对应的目录生成该JAVA文件,目录结构就是stub/src/cn.com.webxml
4、把该JAVA文件导入到ECLIPSE,注意eclipse的java project的buildpath加入%AXIS2_HOME%/lib下所有的JAR。不然会编译出错。
5、写自己的客户端
package com.citigroup.ws.client;
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 cn.com.webxml.WeatherWebServiceStub;
import cn.com.webxml.WeatherWebServiceStub.ArrayOfString;
public class WeatherTest {
/**
* @param args
* @throws Exception
* @throws AxisFault
*/
public static void main(String[] args) throws Exception {
//2nd method to get weather
WeatherWebServiceStub stub = new WeatherWebServiceStub();
WeatherWebServiceStub.GetWeatherbyCityName getWeatherByCityName = new WeatherWebServiceStub.GetWeatherbyCityName();
getWeatherByCityName.setTheCityName("上海");
WeatherWebServiceStub.GetWeatherbyCityNameResponse rs = stub
.getWeatherbyCityName(getWeatherByCityName);
ArrayOfString arry = rs.getGetWeatherbyCityNameResult();
String[] weatherArr = arry.getString();
for (String result : weatherArr) {
System.out.println(result);
}
}
方法2:不生成stub代码,通过OMElement 和endpoint访问
1、导入所需的axis2的jar
2、直接写客户端
package com.citigroup.ws.client;
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 cn.com.webxml.WeatherWebServiceStub;
import cn.com.webxml.WeatherWebServiceStub.ArrayOfString;
public class WeatherTest {
/**
* @param args
* @throws Exception
* @throws AxisFault
*/
public static void main(String[] args) throws Exception {
//first method to get weather
// WeatherTest s = new WeatherTest();
// s.getResult();
System.out.println("---------------------------");
}
private static EndpointReference targetEPR = new EndpointReference(
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
public void getResult() throws Exception {
ServiceClient sender = new ServiceClient();
sender.setOptions(buildOptions());
OMElement result = sender.sendReceive(buildParam());
System.out.println(result);
}
private static OMElement 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 如果不是通过代理上网,此句可省
// options.setProperty(HTTPConstants.PROXY, buildProxy());
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
return options;
}
}