soap 1.1 访问服务

 

访问http://www.webxml.com.cn/zh_cn/web_services_item.aspx?id=494536374B66307964534D3D 可以获取一些外网提供的服务。

 

例如:天气预报

 

2500多个城市天气预报 WEB服务

Endpoint: http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx 复制 Endpoint
Disco: http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?disco 复制 Disco
WSDL: http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 复制 WSDL
2500多个城市天气预报 WEB服务2500多个城市天气预报Web服务,包含2400个以上中国城市和100个以上国外城市天气预报数据。数据每2.5小时左右自动更新一次,准确可靠。为让更多的开发人员学习WEB服务开发,此服务支持免费用户使用。为支持多种平台开发,此WEB服务接口提供了多种返回类型可选择。

复制wsdl里的网址   http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl

cmd中输入  wsimport -s . http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl

出现了问题,这是由于网站提供的xml有点问题,不过不影响我们使用。

把http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 里面的内容保存到磁盘上

e:/test/WeatherWS.xml 然后手工改下错误一般删掉出错的地方

然后运行命令 wsimport -s . WeatherWS.xml 生成了 相应的文件夹,把其中的*.class文件删除

复制 文件夹(包含*.java) 到工程中

然后写代码调用webservice服务

查看 网址 中的service ,看结构从地向上看

<wsdl:service name="WeatherWS">

<wsdl:port name="WeatherWSSoap" binding="tns:WeatherWSSoap">

 

 

所以写出代码

 1 package cn.com.webxml;
 2 
 3 import java.util.List;
 4 
 5 public class AppMain {
 6     public static void main(String [] args)
 7     {
 8         WeatherWS ws=new WeatherWS();
 9         WeatherWSSoap soap=ws.getWeatherWSSoap();
10         ArrayOfString array=soap.getWeather("杭州", null);
11         List<String >list=array.getString();
12         for(String str:list)
13         {
14             System.out.println(str);
15         }
16     }
17 }

但是要注意,因为这是我们从e:/test/Weather.xml那里拿来的文件所以需要到

拷过来的WeatherWS.java那里 把e:/test/Weather.xml 都改成

http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl

才能保证从网上下载来的数据 而不是从e:/test/Weather.xml那里的数据

 

posted on 2014-06-23 21:11  wf110  阅读(1308)  评论(0编辑  收藏  举报