Now we start with the practice of client development, from the client practice to understand the WebService application scenario. For example, there is already a WebService service, providing functional services for translation (mainly the free WebService interface is now blocked), we need to write a client to use this public network to access the WebService services. first, the goal:
Invoke the WebService service by writing a section of client code
Master the general steps and procedures for developing clients second, client development 2.1 First understand the WebService of service delivery
Http://fy.webxml.com.cn/webservices/EnglishChinese.asmx This site A WebService service provider, directly open the URL, you can see the following interface:
We can click on the interface (service) to see if we can work properly
Click on the call to see the string returned by this interface
2.2 Generating WSDL files
The previous section verifies that this WebService interface works, and next, we need to copy the interface documentation for this service to local, after the URL of asmx, plus. WSDL can open the WSDL information page in a browser. http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl
After this URL is opened, it renders the following information
Save this to your local directory (if you have a saved as menu with Chrome, right-click), note that the saved suffix is changed to WSDL 2.3 WSDL document compilation
The compilation of a WSDL document requires a Java-wsimport program to enter the directory in which the WSDL document resides (note that the directory name cannot have spaces, because the command line action takes the space as a delimiter for the argument). In this example, put the TMP directory of e disk, after entering this directory through the command line, execute the following command: C:\jdk1.6.0_43\bin\wsimport-keep EnglishChinese.asmx.wsdl
Ran an error, 19 lines and 105 rows, there are reporting schema issues. We can do a replacement for the information in this location modify part of the WSDL document: replace the <s:element ref= "S:schema"/><s:any/> with <s:any minoccurs= "2" maxoccurs= "2"/>
After the replacement is complete, we execute the last command again, and now we don't have an error.
2.4 Establish client project
First set up a project, STUDYWEBSERVICECLI, in this project, will just copy the interface package in
Then, create a client application class Translatewscli
Package com.study.webservice.client;
Import java.util.List;
Import cn.com.webxml.ArrayOfString;
Import Cn.com.webxml.EnglishChinese;
Import Cn.com.webxml.EnglishChineseSoap;
public class Translatewscli {public
static void Main (string[] args) {
//Create a Englishchinese factory
Englishchinese factory = new Englishchinese ();
Create a Englishchinesesoap object according to the factory
englishchinesesoap translatewssoap = Factory.getenglishchinesesoap ();
Invoke the Translatorsentencestring method provided by WebService to get "work" in Chinese and English sentences
arrayofstring translateinfo = Translatewssoap
. Translatorsentencestring ("work");
Converts the Arrayofstring object to List
list<string> liststring = translateinfo.getstring ();
Traverse the translated info for
(String stringinfo:liststring) {
System.out.println (stringinfo);
System.out.println ("------------");}}
Now we're going to run it and we can see that it's consistent with what's returned in the 2.1 soap test.
Third, summary
The development steps for the WebService client are as follows:
1. Get the ASMX of the Web service and get the WSDL document through this
2, the use of Wsimport tools to import WSDL documents, access to the interface of the Java package or file
3, the establishment of a project, the introduction of the interface package generated in the previous step, and the client development
General Steps for Client program development
1 Create an Interface service factory
2 Get the SOAP interface from this factory
3) Call this interface to get the returned object
4) data processing of the returned object