The internet has a lot of free webservice services, we can call these free webservice services, to integrate some other website content information into our Web application display, the following to obtain weather forecast data and query domestic mobile phone number attribution Place As an example to illustrate.
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 information that provides the weather forecast
Webservice
the network address
The Http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx access results are as follows:
650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291803534729397.png "style=" border:0px; " />
Find the WSDL description for WebService, via the URL address http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl: as follows:
650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291807011917980.png "style=" border:0px; " />
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:
650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291812178942002.png "style=" border:1px Solid #000000; "/>
Open a command-line window, switch to the project's SRC directory, and execute "wsimport-keep http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl" Generate the client code as shown in:
650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291816094411874.png "style=" border:0px; " />
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
650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291843172539113.gif "style=" border:0px; " />
2. Modify part of the WSDL document: replace <s:element ref= "S:schema"/><s:any/> with <s:any minoccurs= "2" maxoccurs= "2"/>
650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291851362693798.gif "style=" border:0px; " />
Execute the Wsimport generated code again, this time using a local weatherws.wsdl file to generate it, as shown in:
650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291833037221243.png "style=" border:0px; " />
From the execution results show, this time can generate code normally, refresh the SRC directory, you can see the generated code, as shown in:
650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291836151125713.png "style=" border:1px Solid #000000; "/>
2. Write the request code with the generated code
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>
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 //Create a WEATHERWS factory 13 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;WEATHERWS&NBSP;FACTORY&NBSP;=&NBSP;NEW&NBSP;WEATHERWS ();14 //creates a Weatherwssoap object based on the factory 15 weatherwssoap weatherwssoap = factory.getweatherwssoap ();16 //calls the GetWeather method provided by WebService to get the weather forecast for Nanning 17 arrayofstrinG weatherinfo = weatherwssoap.getweather ("Nanning", null);18 list<string> lstweatherinfo = weatherinfo.getstring ();19 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//Traverse weather Forecast information 20 for (String string : lstweatherinfo) {21 system.out.println (String);22 system.out.println ("------------------------");23 }24 }25 }
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>
The results of the visit are as follows:
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>
Guangxi Nanning------------------------nanning------------------------2391------------------------2015/01/29 19:00:49------------------------Today's weather live: no live------------------------air quality: No UV intensity: The 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 Wind Breeze------------------------1.gif------------------------1.gif------------------------January 30 Cloudy------------------------10 ℃/ 15℃------------------------Northeast Wind Breeze------------------------7.gif------------------------2.gif------------------------January 31 Cloudy------------------------11 ℃/ 13℃------------------------Northeast Wind Breeze------------------------1.gif------------------------1.gif------------------------February 1 Cloudy------------------------12℃/17℃------------------------Northeast Breeze------------------------1.gif------------------------1.gif------------------------February 2 Overcast to Cloudy------------------------13 ℃/ 18℃------------------------northeast Wind------------------------2.gif------------------------1.gif------------------------
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>
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 Place2.1. Find the network address of the WebService providing the service
The http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx is as follows:
650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291921125037348.png "style=" border:0px; " />
Locate the WSDL description information for WebService, via the URL address http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl:
650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291925001286789.png "style=" border:0px; " />
2.2. Write Client Access call WebService service
1. Create a client project
650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291928306282032.png "style=" border:1px Solid #000000; "/>
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:
650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291933009569318.png "style=" border:0px; " />
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:
650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291935288626070.png "style=" border:1px Solid #000000; "/>
3. Write the request code with the generated code
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>
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 10 MobileCodeWS factory = new Mobilecodews (); 11 //creates a Mobilecodewssoap object based on the factory 12 MobileCodeWSSoap mobileCodeWSSoap = Factory.getmobilecodewssoap (); 13 //// Call the Getmobilecodeinfo method provided by WebService to query the attribution of the mobile phone number 14 string searchresult = mobilecodewssoap.getmobilecodeinfo("15177196635", null); 15 system.out.println ( SearchResult); 16 }17 }
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>
The results of the operation are as follows:
650) this.width=650; "src=" Http://images.cnitblog.com/blog/289233/201501/291943526446545.png "style=" border:1px Solid #000000; "/>
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--Call the WebService service provided by a third party