In this post let me go through the process of creating a web service client in java on ubuntu.
We need to have the apis for programming web services. Java provides a api called JAX-WS which is version 2 of the java web services api the previous version being JAX-RPC. JAX-WS comes a part of JDK6.0 and above. If you are using jdk5 then we need to download the JAX-WS web-service apis. JAX-WS is developed as a separate project called Metro and the latest version can be downloaded from the Metro project site – https://jax-ws.dev.java.net/. you can follow the instructions on the site for installing it.
Once installed it can be checked by tying wsimport at the terminal. It should give an output which looks like this
Usage: wsimport [options]
where [options] include:
-b specify jaxws/jaxb binding files or additional schemas
(Each must have its own -b)
-B Pass this option to JAXB schema compiler
-catalog specify catalog file to resolve external entity references
supports TR9401, XCatalog, and OASIS XML Catalog format.
-d specify where to place generated output files
-extension allow vendor extensions - functionality not specified
by the specification. Use of extensions may
result in applications that are not portable or
may not interoperate with other implementations
-help display help
-httpproxy:: specify a HTTP proxy server (port defaults to 8080)
-keep keep generated files
-p specifies the target package
-quiet suppress wsimport output
-s specify where to place generated source files
-target generate code as per the given JAXWS specification version.
version 2.0 will generate compliant code for JAXWS 2.0 spec.
-verbose output messages about what the compiler is doing
-version print version information
-wsdllocation @WebServiceClient.wsdlLocation value
Examples:
wsimport stock.wsdl -b stock.xml -b stock.xjb
wsimport -d generated http://example.org/stock?wsdl
If you get an output like this then we are done with the installation. we can proceed to the next step. Otherwise refer tot he metro site for installing this. For people using jdk6 everything is set by default and they can verify their installation by typing the above command in a terminal
Now we will find a site which provides free web service. I found a site http://www.webservicex.net/ which hosts lot of free web services organized into different categories. For this example lets us take a service which is called WHOIS web service which is available in the utilities section. The url for this is – http://www.webservicex.net/WCF/ServiceDetails.aspx?SID=51
Now we have to get the WSDL for this web service and compile it using the wsimport utility to get the start-up code for our web service.
To do this open a terminal and type
wsimport -keep -verbose -d whois http://www.webservicex.net/whois.asmx?wsdl
here the -keep tells the command to keep the generated java files and the -d is used to specify the output directory. we would get a output like this
parsing WSDL...
[WARNING] Ignoring SOAP port "whoisSoap12": it uses non-standard SOAP 1.2 binding.
You must specify the "-extension" option to use this binding.
line 113 of http://www.webservicex.net/whois.asmx?wsdl
[WARNING] ignoring port "whoisHttpGet": no SOAP address specified. try running wsimport with -extension switch.
line 116 of http://www.webservicex.net/whois.asmx?wsdl
[WARNING] ignoring port "whoisHttpPost": no SOAP address specified. try running wsimport with -extension switch.
line 119 of http://www.webservicex.net/whois.asmx?wsdl
generating code...
net/webservicex/GetWhoIS.java
net/webservicex/GetWhoISResponse.java
net/webservicex/ObjectFactory.java
net/webservicex/Whois.java
net/webservicex/WhoisSoap.java
net/webservicex/package-info.java
Now go to the directory where these files are created and we can create the client.
Create a java file with the following contents
package net.webservicex;
public class WhoisClient{
public static void main(String args[]){
Whois whois=new Whois();
WhoisSoap whoisSoap=whois.getWhoisSoap();
System.out.println(whoisSoap.getWhoIS("www.google.com"));
}
}
or still more usable as
package net.webservicex;
public class WhoisClient{
public static void main(String args[]){
Whois whois=new Whois();
WhoisSoap whoisSoap=whois.getWhoisSoap();
System.out.println(whoisSoap.getWhoIS(args[0]));
}
}
That is it we are donw with our web service client. Now compile the program by setting the classpath to point to the directory that contains the generated class files using the command
javac WhoisClient.java
Now the program can be run with by typing this in the terminal
java net.webservicex.WhoisClient www.google.com
we would get an output as below
Whois Server Version 2.0
Domain names in the .com and .net domains can now be registered
with many different competing registrars. Go to http://www.internic.net
for detailed information.
WWW.GOOGLE.COM.VN
WWW.GOOGLE.COM.TW
WWW.GOOGLE.COM.TR
WWW.GOOGLE.COM.SA
WWW.GOOGLE.COM.PE
WWW.GOOGLE.COM.MX
WWW.GOOGLE.COM.CO
WWW.GOOGLE.COM.BR
WWW.GOOGLE.COM.AU
WWW.GOOGLE.COM.AR
To single out one record, look it up with "xxx", where xxx is one of the
of the records displayed above. If the records are the same, look them up
with "=xxx" to receive a full display for each record.
>>> Last update of whois database: Sat, 05 Sep 2009 06:29:11 UTC <<<
NOTICE: The expiration date displayed in this record is the date the
registrar's sponsorship of the domain name registration in the registry is
currently set to expire. This date does not necessarily reflect the expiration
date of the domain name registrant's agreement with the sponsoring
registrar. Users may consult the sponsoring registrar's Whois database to
view the registrar's reported date of expiration for this registration.
TERMS OF USE: You are not authorized to access or query our Whois
database through the use of electronic processes that are high-volume and
automated except as reasonably necessary to register domain names or
modify existing registrations; the Data in VeriSign Global Registry
Services' ("VeriSign") Whois database is provided by VeriSign for
information purposes only, and to assist persons in obtaining information
about or related to a domain name registration record. VeriSign does not
guarantee its accuracy. By submitting a Whois query, you agree to abide
by the following terms of use: You agree that you may use this Data only
for lawful purposes and that under no circumstances will you use this Data
to: (1) allow, enable, or otherwise support the transmission of mass
unsolicited, commercial advertising or solicitations via e-mail, telephone,
or facsimile; or (2) enable high volume, automated, electronic processes
that apply to VeriSign (or its computer systems). The compilation,
repackaging, dissemination or other use of this Data is expressly
prohibited without the prior written consent of VeriSign. You agree not to
use electronic processes that are automated and high-volume to access or
query the Whois database except as reasonably necessary to register
domain names or modify existing registrations. VeriSign reserves the right
to restrict your access to the Whois database in its sole discretion to ensure
operational stability. VeriSign may restrict or terminate your access to the
Whois database for failure to abide by these terms of use. VeriSign
reserves the right to modify these terms at any time.
The Registry database contains ONLY .COM, .NET, .EDU domains and
Registrars.
Here we can give different host names as argument and experiment with it. There are many free web services available at http://www.webservicex.net and almost all of them can be consumed using the same method by using java clients. Thanks for reading