WebService Study Summary (iv)--Call the WebService service provided by a third party
The internet has a lot of free webservice services, we can call these free webservice services, some other website content information integrated into our Web application display, the following is to obtain weather forecast data and query domestic mobile phone number attribution place for example to explain.
Meteorological Center Management system will collect the weather information and expose the data (through WebService Server), and the application of the major sites to call them to get the weather information and display in different styles (WebService Client).
First, call the free web service to get weather forecast information
1.1. Find the network address of the webservice that provides the weather forecast information
The Http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx access results are as follows:
Find the WSDL description for WebService, via the URL address http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl: as follows:
1.2. Client-side encoding access to WebService services provided by third parties
1. Automatically generate client code with the Wsimport command tool
Create a test project, as follows:
Open a command line window, switch to the project's SRC directory, execute "wsimport-keep http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl" to generate the client code, As shown in the following:
Running results show that using the Wsimport tool to generate the client code directly throws an exception, unable to generate the client code, Just because the webservice we're going to call is written in. NET, this is the problem with the Java call Net WebService, the solution to this problem is as follows:
Save the corresponding WSDL document to a local
2. Modify part of the WSDL document: replace <s:element ref= "S:schema"/><s:any/> with <s:any minoccurs= "2" maxoccurs= "2"/>
Execute the Wsimport generated code again, this time using a local weatherws.wsdl file to generate it, as shown in:
From the execution results show, this time can generate code normally, refresh the SRC directory, you can see the generated code, as shown in:
2. Write the request code with the generated code
Copy 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
public static void Main (string[] args) {
12//Create a WEATHERWS factory
WEATHERWS factory = new Weatherws ();
14//Create a Weatherwssoap object according to the factory
Weatherwssoap Weatherwssoap = Factory.getweatherwssoap ();
16//Call the GetWeather method provided by WebService to get the weather forecast for Nanning
Arrayofstring weatherinfo = Weatherwssoap.getweather ("Nanning", null);
list<string> lstweatherinfo = weatherinfo.getstring ();
19//Traverse Weather forecast information
(String string:lstweatherinfo) {
System.out.println (string);
System.out.println ("------------------------");
23}
24}
25}
Copy Code
The results of the visit are as follows:
Copy Code
Nanning, Guangxi
------------------------
Nanning
------------------------
2391
------------------------
2015/01/29 19:00:49
------------------------
Today's weather fact: no live
------------------------
Air quality: No, UV intensity: weakest
------------------------
Sunglasses index: not required. Daytime light is weak and no sunglasses are required.
Clothing index: more comfortable. Wear clothing such as a thin coat or jeans is recommended.
Tourism index: suitable. The temperature is suitable, can enjoy the natural scenery.
Movement Index: more suitable. It is more suitable to carry out various indoor and outdoor movements.
Car Wash Index: not appropriate. Rain, rain and mud will stain your car.
Makeup index: Moisturizing. Please choose a neutral moisturizing cream type cosmetics.
Cold index: more prone to hair. Days cooler, increase clothing, pay attention to protection.
Air Pollution index: not currently.
UV index: Weakest. Radiation is weak, rub spf8-12 sunscreen skin care products.
Comfort Index: comfortable. The daytime is lukewarm and the wind is small.
------------------------
January 29 Cloudy
------------------------
11℃/15℃
------------------------
Northeast Breeze
------------------------
1.gif
------------------------
1.gif
------------------------
January 30 Rain Turn Yin
------------------------
10℃/15℃
------------------------
Northeast Breeze
------------------------
7.gif
------------------------
2.gif
------------------------
January 31 Cloudy
------------------------
11℃/13℃
------------------------
Northeast Breeze
------------------------
1.gif
------------------------
1.gif
------------------------
February 1 Cloudy
------------------------
12℃/17℃
------------------------
Northeast Breeze
------------------------
1.gif
------------------------
1.gif
------------------------
February 2 Overcast Turn Cloudy
------------------------
13℃/18℃
------------------------
Northeast Breeze
------------------------
2.gif
------------------------
1.gif
------------------------
Copy Code
In this way, we call the WebService service provided by the third party through the generated client code to get the weather information for the Nanning.
Second, call the free Web service query domestic mobile phone number attribution place
2.1. Find the network address of the WebService providing the service
The http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx is as follows:
Locate the WSDL description information for WebService, via the URL address http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl:
2.2. Write Client Access call WebService service
1. Create a client project
2. Generate client code according to WebService WSDL description
Open a command-line window, switch to Phone_client's SRC directory, and execute Wsimport-keep http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl as shown:
This time you can generate code directly using WSDL, no longer need to download the WSDL file locally for modification, this webservice should not be written in. NET, the generated code is as follows:
3. Write the request code with the generated code
Copy Code
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//Create a mobilecodews factory
Ten Mobilecodews factory = new Mobilecodews ();
11//Create a Mobilecodewssoap object according to the factory
Mobilecodewssoap Mobilecodewssoap = Factory.getmobilecodewssoap ();
13////Call WebService provides the Getmobilecodeinfo method to query the cell phone number attribution
String SearchResult = Mobilecodewssoap.getmobilecodeinfo ("15177196635", null);
System.out.println (SearchResult);
16}
17}
Copy Code
The results of the operation are as follows:
This allows us to invoke the WebService service provided by the third party to successfully query the destination of the mobile phone number.
The above is how to invoke the third-party webservice related content.
WebService Study Summary (iv)--Call the WebService service provided by a third party