axis2 远程调用教程

 
 

Web服务入门之二:通过axis2调用远程Web Service,实现天气预报功能

分类: Web Service   840人阅读   评论(1)   收藏   举报

今天我们来实现一个通过AXIS2来调用中国气象局的一个实现天气预报功能的Web Service。

 

准备工作:

1.下载axis2-1.4.1的bin包,地址:http://apache.etoak.com/ws/axis2/1_5/axis2-1.5-bin.zip

2.解压,将lib目录下的所有的jar包拷贝出来,在这里建议大家在自己的电脑上专门建立一个文件夹放jar包,比如:jdbc的jar包,Struts的jar包,spring的jar包等等,这里,我们将axis2的jar包专门拷贝出来放在一个目录下。

 

接下来的步骤是:

1、new java project 

2、引入刚才axis2的所有jar包

3、new class

参考代码如下:

 

  1. package com.weather.webservice;  
  2.   
  3. import org.apache.axiom.om.OMAbstractFactory;  
  4. import org.apache.axiom.om.OMElement;  
  5. import org.apache.axiom.om.OMFactory;  
  6. import org.apache.axiom.om.OMNamespace;  
  7. import org.apache.axiom.soap.SOAP11Constants;  
  8. import org.apache.axis2.Constants;  
  9. import org.apache.axis2.addressing.EndpointReference;  
  10. import org.apache.axis2.client.Options;  
  11. import org.apache.axis2.client.ServiceClient;  
  12. import org.apache.axis2.transport.http.HTTPConstants;  
  13. import org.apache.axis2.transport.http.HttpTransportProperties.ProxyProperties;  
  14. import org.apache.commons.logging.Log;  
  15. import org.apache.commons.logging.LogFactory;  
  16.   
  17. public class Test  
  18. {  
  19.     private static EndpointReference targetEPR = new EndpointReference(  
  20.             "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");  
  21.   
  22.     public void getResult() throws Exception  
  23.     {  
  24.         ServiceClient sender = new ServiceClient();  
  25.         sender.setOptions(buildOptions());  
  26.         OMElement result = sender.sendReceive(buildParam());  
  27.         System.out.println(result);  
  28.     }  
  29.   
  30.     private static OMElement buildParam()  
  31.     {  
  32.         OMFactory fac = OMAbstractFactory.getOMFactory();  
  33.         OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/""");  
  34.         OMElement data = fac.createOMElement("getWeatherbyCityName", omNs);  
  35.         OMElement inner = fac.createOMElement("theCityName", omNs);  
  36.         inner.setText("北京");  
  37.         data.addChild(inner);  
  38.         return data;  
  39.     }  
  40.   
  41.     private static Options buildOptions()  
  42.     {  
  43.         Options options = new Options();  
  44.         options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);  
  45.         options.setAction("http://WebXml.com.cn/getWeatherbyCityName");  
  46.         options.setTo(targetEPR);  
  47.         //options.setProperty 如果不是通过代理上网,此句可省  
  48.         //options.setProperty(HTTPConstants.PROXY, buildProxy());  
  49.         options.setTransportInProtocol(Constants.TRANSPORT_HTTP);  
  50.         return options;  
  51.     }  
  52.   
  53.     /** 
  54.      * 本机采用代理服务器上网时,需要设置代理 
  55.      * @return 
  56.      */  
  57.     public static ProxyProperties buildProxy()  
  58.     {  
  59.         ProxyProperties proxyProperties = new ProxyProperties();  
  60.         proxyProperties.setProxyName("代理名称");  
  61.         proxyProperties.setProxyPort(8080);  
  62.         return proxyProperties;  
  63.     }  
  64.   
  65.     public static void main(String[] args) throws Exception  
  66.     {  
  67.         Test s = new Test();  
  68.         s.getResult();  
  69.     }  
  70.   
  71. }  

 

Result:

- Deploying module: metadataExchange-1.5 - file:/D:/jars/axis2/mex-1.5.jar
- Required credentials not available for BASIC <any realm>@www.webxml.com.cn:80
- Preemptive authentication requested but no default credentials available
<getWeatherbyCityNameResponse xmlns="http://WebXml.com.cn/"><getWeatherbyCityNameResult><string>直辖市</string><string>北京</string><string>54511</string><string>54511.jpg</string><string>2010-4-10 15:45:38</string><string>4℃/15℃</string><string>4月10日 多云转阴</string><string>无持续风向微风</string><string>1.gif</string><string>2.gif</string><string>今日天气实况:气温:15℃;风向/风力:西南风 小于3级;湿度:14%;气压:1011.6hPa;空气质量:中;紫外线强度:最弱</string><string>穿衣指数:建议着夹衣加薄羊毛衫等春秋服装。体弱者宜着夹衣加羊毛衫。但昼夜温差较大,注意增减衣服。
感冒指数:天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。
晨练指数:天气晴朗,空气清新,是您晨练的大好时机,建议不同年龄段的人们积极参加户外健身活动。
洗车指数:不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。
晾晒指数:天气晴朗,午后温暖的阳光仍能满足你驱潮消霉杀菌的晾晒需求。
旅游指数:多云,有时云会遮挡住阳光,但丝毫不会影响您出行的心情。温度适宜又有微风相伴,很适宜旅游。
路况指数:晴天,其它条件适宜,路面比较干燥,路况较好。
舒适度指数:白天不太热也不太冷,风力不大,相信您在这样的天气条件下,应会感到比较清爽和舒适。</string><string>4℃/9℃</string><string>4月11日 小到中雨转小雨</string><string>无持续风向微风转北风3-4级</string><string>21.gif</string><string>7.gif</string><string>3℃/11℃</string><string>4月12日 晴</string><string>北风4-5级</string><string>0.gif</string><string>0.gif</string><string>北京位于华北平原西北边缘,市中心位于北纬39度,东经116度,四周被河北省围着,东南和天津市相接。全市面积一万六千多平方公里,辖12区6县,人口1100余万。北京为暖温带半湿润大陆性季风气候,夏季炎热多雨,冬季寒冷干燥,春、秋短促,年平均气温10-12摄氏度。北京是世界历史文化名城和古都之一。早在七十万年前,北京周口店地区就出现了原始人群部落“北京人”。而北京建城也已有两千多年的历史,最初见于记载的名字为“蓟”。公元前1045年北京成为蓟、燕等诸侯国的都城;公元前221年秦始皇统一中国以来,北京一直是中国北方重镇和地方中心;自公元938年以来,北京又先后成为辽陪都、金上都、元大都、明清国都。1949年10月1日正式定为中华人民共和国首都。北京具有丰富的旅游资源,对外开放的旅游景点达200多处,有世界上最大的皇宫紫禁城、祭天神庙天坛、皇家花园北海、皇家园林颐和园,还有八达岭、慕田峪、司马台长城以及世界上最大的四合院恭王府等各胜古迹。全市共有文物古迹7309项,其中国家文物保护单位42个,市级文物保护单位222个。北京的市树为国槐和侧柏,市花为月季和菊花。另外,北京出产的象牙雕刻、玉器雕刻、景泰蓝、地毯等传统手工艺品驰誉世界。</string></getWeatherbyCityNameResult></getWeatherbyCityNameResponse>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值