Here you'll have complete example on how to invoke a web-service, in a simple Java client, using SOAP as a messaging protocol over HTTP, given the webservice WSDL file of the previous post. That may help you better understand webservice / client communication in distributed systems.
Client will read SOAP request from a local xml file, then make HTTP connection to webservice url and send SOAP message over the given connection.
First, lets design the xml SOAP request, to invoke sayHi method of the previous webservice:
- Envelope element: root element of SOAP request, contains namespace declaration of soap; xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
- Body: contains elements of actual request message (with fully qualified name), i.e: the method to be invoked and input parameters if any,
<sayHi xmlns="http://test">
<name>clientName</name>
</sayHi>
Notes:
* namespace uri is the same one used to define target namespace in wsdl
* child element of method is the input parameter of sayHi; which is called "name" of type string. (see previous post)
SOAP request looks like this:
<?xml version="1.0" ?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<sayHi xmlns="http://test">
<name>clientName</name>
</sayHi>
</soapenv:Body>
</soapenv:Envelope>
lets save soap request in xml file, say at C:/soapclient.xml
To complete SOAP request, we will add the following to http header (using POST method):
* Content-Length : -length of request data-
* Content-Type: text/xml; charset=utf-8
* SOAPAction: "" (as mentioned in wsdl, inside soap:binding of 'sayHi' operation)
Client code will open fileInput stream to read C:/soapclient.xml, and send the content to webservice URL: http://localhost:8080/service/services/MainService
(as mentioned in wsdl under <wsdlsoap:address>)
public class MainClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String urlStr=
"http://localhost:8080/service/services/MainService";
try {
URL url=new URL(urlStr);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
FileInputStream fin = new FileInputStream("C:/soapclient.xml");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
// Copy the SOAP file to the open connection.
copy(fin,bout);
fin.close();
byte[] b = bout.toByteArray();
// Set the appropriate HTTP parameters.
conn.setRequestProperty( "Content-Length",
String.valueOf( b.length ) );
conn.setRequestProperty("Content-Type",
"text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction","");
conn.setRequestMethod( "POST" );
conn.setDoOutput(true);
conn.setDoInput(true);
// Everything's set up; send the XML that was read in to b.
OutputStream out = conn.getOutputStream();
out.write( b );
out.close();
// Read the response and write it to standard out.
InputStreamReader isr =
new InputStreamReader(conn.getInputStream());
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
// copy method from From E.R. Harold's book "Java I/O"
public static void copy(InputStream in, OutputStream out)
throws IOException {
// do not allow other threads to read from the
// input or write to the output while copying is
// taking place
synchronized (in) {
synchronized (out) {
byte[] buffer = new byte[256];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1) break;
out.write(buffer, 0, bytesRead);
}
}
}
}
}
Output of client will show SOAP response in console screen:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<sayHiResponse xmlns="http://test">
<sayHiReturn>hi clientName</sayHiReturn>
</sayHiResponse>
</soapenv:Body>
</soapenv:Envelope>
Resources:
http://www.ibm.com/developerworks/xml/library/x-soapcl/
http://www.ibm.com/developerworks/xml/library/x-soapcl/listing1.html
http://www.ibm.com/developerworks/webservices/library/ws-intwsdl2/
Next, I'm going to show how to generate and invoke client using Axis2 apis.
Sunday, May 11, 2008
Subscribe to:
Post Comments (Atom)
7 comments:
Nice post.
I still prefer the Axis2 :)
Regards
i now prefer Axis2 :)
but, i'm searching for a solution
to pass complex types when invoking a webservice,
till now i find the response in XML format :(
(sure any help would be appreciated..)
Thanks
I think you should check to use data binding with axis
there is 3 famous data binding framewroks could use with axis
1)ADB
2)XMLBeans
3)Jibx
for more info check
http://www.ibm.com/developerworks/webservices/library/ws-java3/
جزااك الله خيرا
i'll check that asap,
Thanks
:)
What a great web log. I spend hours on the net reading blogs, about tons of various subjects. I have to first of all give praise to whoever created your theme and second of all to you for writing what i can only describe as an fabulous article. I honestly believe there is a skill to writing articles that only very few posses and honestly you got it. The combining of demonstrative and upper-class content is by all odds super rare with the astronomic amount of blogs on the cyberspace.
Great post!
Thanks
Dharmi
Great Post!
Though several alternatives exists, this simplifies writing a test client with no addons.
Thanks,
Dharmi
Post a Comment