WebService Study Summary (iv)--Call the WebService service provided by a third party

Source: Internet
Author: User
Tags wsdl

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:

  

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, and execute "wsimport-keep http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl" Generate the client code as shown in:

  

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:

    1. 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

1  Packageme.gacl.ws.client;2 3 Importjava.util.List;4 5 Importcn.com.webxml.ArrayOfString;6 ImportCn.com.webxml.WeatherWS;7 ImportCn.com.webxml.WeatherWSSoap;8 9  Public classweatherwsclient {Ten  One      Public Static voidMain (string[] args) { A         //Create a WEATHERWS factory -WEATHERWS factory =NewWeatherws (); -         //Create a Weatherwssoap object based on the factory theWeatherwssoap Weatherwssoap =Factory.getweatherwssoap (); -         //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 (); +         //Traverse weather Forecast information -          for(String string:lstweatherinfo) { + System.out.println (string); ASystem.out.println ("------------------------"); at         } -     } -}

The results of the visit are as follows:

------------------------nanning------------------------2391------------------------2015/01/29 19:00:49, Nanning, Guangxi---------- --------------Weather today: 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 Wind Breeze------------------------1.gif------------------------1.gif------------------------January 30 Drizzle turn Yin------------------------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 wind------------------------1.gif------------------------1.gif------------------------February 2 Overcast to cloudy------------------------13 ℃/  18℃------------------------northeast Wind------------------------2.gif------------------------1.gif------------------------

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:

  

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

1  Packageme.gacl.ws.client;2 3 ImportCn.com.webxml.MobileCodeWS;4 ImportCn.com.webxml.MobileCodeWSSoap;5 6  Public classphonewsclient {7 8      Public Static voidMain (string[] args) {9         //Create a mobilecodews factoryTenMobilecodews factory =Newmobilecodews (); One         //Create a Mobilecodewssoap object based on the factory AMobilecodewssoap Mobilecodewssoap =Factory.getmobilecodewssoap (); -         ////call the Getmobilecodeinfo method provided by WebService to query the place where the phone number belongs. -String SearchResult = Mobilecodewssoap.getmobilecodeinfo ("15177196635",NULL); the System.out.println (SearchResult); -     } -}

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.