Call the webService service provided by the third party

  There are many free webservice services on the Internet. We can call these free webservice services and integrate the content information of some other websites into our web application for display. The following is an example of obtaining weather forecast data and querying the attribution of domestic mobile phone numbers . Be explained.

  The management system of the weather center will collect the weather information and expose the data (through the WebService Server), and the applications of major sites will call them to get the weather information and display it in different styles (WebService Client).

1. Call a free web service to obtain weather forecast information

1.1. Find the network address of the Webservice that provides weather forecast information

  The result of accessing http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx  is shown in the following figure:

  

  Find the wsdl description of WebService, through the URL address http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl : as follows:

  

1.2. Client-side coding to access webService services provided by third parties

  1. Automatically generate client code with the help of the wsimport command tool

  Create a test project as follows:

  

  Open the command line window, switch to the src directory of the project, and execute " wsimport -keep  http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl " to generate the client code, as shown in the following figure:

  

  The running results show that using the wsimport tool to directly generate client code will throw an exception, and the client code cannot be generated, just because the WebService we want to call is written in .net, which is a problem with Java calling net webservices. The solution is as follows:

  1. Save the corresponding wsdl document to the local

  

  2. Modify part of the wsdl document: replace  <s:element ref="s:schema" /><s:any />  with  <s:any minOccurs="2" maxOccurs="2"/>

  

  Execute wsimport again to generate the code, this time using the WeatherWS.wsdl file saved locally, as shown in the following figure:

  

  From the execution result, the code can be generated normally this time. After refreshing the src directory, you can see the generated code, as shown in the following figure:

  

2. Write the request code with the help of the generated code

复制代码
 1 package me.gacl.ws.client;
 2 
 3 import java.util.List;
 4 
 5 import cn.com.webxml.ArrayOfString;
 6 import cn.com.webxml.WeatherWS;
 7 import cn.com.webxml.WeatherWSSoap;
 8 
 9 public class WeatherWSClient {
10 
11     public static void main(String[] args) {
12         //创建一个WeatherWS工厂
13         WeatherWS factory = new WeatherWS();
14         //根据工厂创建一个WeatherWSSoap对象
15         WeatherWSSoap weatherWSSoap = factory.getWeatherWSSoap();
16         //调用WebService提供的getWeather方法获取南宁市的天气预报情况
17         ArrayOfString weatherInfo = weatherWSSoap.getWeather("南宁", null);
18         List<String> lstWeatherInfo = weatherInfo.getString();
19         //遍历天气预报信息
20         for (String string : lstWeatherInfo) {
21             System.out.println(string);
22             System.out.println("------------------------");
23         }
24     }
25 }
复制代码

  访问结果如下:

复制代码
广西 南宁
------------------------
南宁
------------------------
2391
------------------------
2015/01/29 19:00:49
------------------------
今日天气实况:暂无实况
------------------------
空气质量:暂无;紫外线强度:最弱
------------------------
太阳镜指数:不需要。白天光线弱不需要佩戴太阳镜。
穿衣指数:较舒适。建议穿薄外套或牛仔裤等服装。
旅游指数:适宜。温度适宜,可尽情享受大自然风光。
运动指数:较适宜。较适宜进行各种户内外运动。
洗车指数:不宜。有雨,雨水和泥水会弄脏爱车。
化妆指数:保湿。请选用中性保湿型霜类化妆品。
感冒指数:较易发。天较凉,增加衣服,注意防护。
空气污染指数:暂无。
紫外线指数:最弱。辐射弱,涂擦SPF8-12防晒护肤品。
舒适度指数:舒适。白天不冷不热,风力不大。

------------------------
1月29日 多云
------------------------
11℃/15℃
------------------------
东北风微风
------------------------
1.gif
------------------------
1.gif
------------------------
1月30日 小雨转阴
------------------------
10℃/15℃
------------------------
东北风微风
------------------------
7.gif
------------------------
2.gif
------------------------
1月31日 多云
------------------------
11℃/13℃
------------------------
东北风微风
------------------------
1.gif
------------------------
1.gif
------------------------
2月1日 多云
------------------------
12℃/17℃
------------------------
东北风微风
------------------------
1.gif
------------------------
1.gif
------------------------
2月2日 阴转多云
------------------------
13℃/18℃
------------------------
东北风微风
------------------------
2.gif
------------------------
1.gif
------------------------
复制代码

  这样,我们通过生成的Client代码调用了第三方提供的webService服务获取到了南宁市的天气预报信息。

二、 调用免费的web service查询国内手机号码归属地

2.1、找到提供服务的Webservice的网络地址

  http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx 如下所示:

  

  找到WebService的wsdl描述信息,通过URL地址http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl:如下:

  

2.2、编写客户端访问调用WebService服务

1、创建客户端项目

  

2、根据WebService的wsdl描述生成客户端代码

  打开命令行窗口,切换到Phone_Client的src目录,执行wsimport -keephttp://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl 如下图所示:

  

  这次可以直接使用wsdl生成代码,不再需要将wsdl文件下载到本地进行修改了,这个WebService应该不是使用.net写的,生成的代码如下:

  

  3、借助生成的代码编写请求代码

复制代码
 1 package me.gacl.ws.client;
 2 
 3 import cn.com.webxml.MobileCodeWS;
 4 import cn.com.webxml.MobileCodeWSSoap;
 5 
 6 public class PhoneWsClient {
 7 
 8     public static void main(String[] args) {
 9         //创建一个MobileCodeWS工厂
10         MobileCodeWS factory = new MobileCodeWS();
11         //根据工厂创建一个MobileCodeWSSoap对象
12         MobileCodeWSSoap mobileCodeWSSoap = factory.getMobileCodeWSSoap();
13         ////调用WebService提供的getMobileCodeInfo方法查询手机号码的归属地
14         String searchResult = mobileCodeWSSoap.getMobileCodeInfo("15177196635", null);
15         System.out.println(searchResult);
16     }
17 }
复制代码

  运行结果如下:
  

  这样我们调用第三方提供的WebService服务成功查询到了手机号码的归宿地。

  以上就是如何调用第三方WebService的相关内容。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326656011&siteId=291194637