File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services and the fly likes httpclient for accessing webservice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "httpclient for accessing webservice" Watch "httpclient for accessing webservice" New topic
Author

httpclient for accessing webservice

Karthik Rajendiran
Ranch Hand

Joined: Aug 13, 2004
Posts: 211
Hello Friends,

can we use httpclient by apache commons to access webservice.

How do we send the soap request to the webservice, should ihave to frame the request and post
or is it enough if i give xml of the document based webserviec alone to server.

1. How to construct the xml with parameters and how to read from the response.

Can anyone help with this?


SCJP 1.4 SCWCD 1.4 SCDJWS 1.4
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5
1. IF the service you need to access is based on the SOAP protocol, THEN there are numerous toolkits which will help you create a client that can create a SOAP formated request and interpret the response. The Apache Axis2 toolkit is one example.

2. HttpClient makes it easy to use sevices based on RESTful designs for creating a request. The standard Java library (java.net package) can also be used. You will have to code the response interpretation.

3. For simple SOAP queries where you have an example SOAP message provided by the service, you can use HttpClient or the standard Java library to generate a request using the example as a template. Again - interpreting the response will require custom code.

Bill
Karthik Rajendiran
Ranch Hand

Joined: Aug 13, 2004
Posts: 211
Thanks sir, for your reply in for httpclient


1. Can i use apache httpclient commons for invoking webservice
2. If yes,
to post soap request,
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://testws.example.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns0:getEmployeeDetails>
<emnpNo>11111</emnpNo>
</ns0:getEmployeeDetails>
</soapenv:Body>
</soapenv:Envelope>


Should i have to give entire message, or just body of the soap, enough,

like <ns0:getEmployeeDetails>
<emnpNo>11111</emnpNo>
</ns0:getEmployeeDetails>

Will httpclient will automatically construct the soap envelopes?


2. Which approach to follow for constructing the soap envelope?
a. KEep stringbuffer and appending the tags?
b. or Keep a string constant with ${0} parameters and replace parameters with the values using messageformat class?

c. or read from xml file and put the values and post it?
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3219
If you want to just test your Web Service, you can look at the Soap UI tool.

http://www.soapui.org/


All, you need to do is install it and invoke your Web Service from this tool by just pasting your SOAP payload.


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Andy
Greenhorn

Joined: Sep 01, 2008
Posts: 2
Hi,

you can use JAX-WS to create client stubs for you web service.
Check out this example

Regards,
Andy


<a href="http://www.javadb.com/" target="_blank" rel="nofollow">Lots of Java Code Examples</a>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5
Will httpclient will automatically construct the soap envelopes?


No, HttpClient is just useful for emulating a browser.

Given that you have a very simple SOAP request and an example message to use as a template, you can use the standard Java library HttpURLConnection to POST the request message. There is no need to mess with a SOAP library for such a simple situation.

When I did this I hard coded the template as a String[] having substituted recognizable strings (like "@vendorkey")where all the user variables would go.

Constructing the SOAP request then consisted of adding the template strings to a StringBuffer, with substitutions for each "@" labeled text.

Here is what the code to send the request and read the response looked like, where reqStr is the filled out template for the complete SOAP request:



The soapActions variable was required by this particular web service, you may not need it.

Bill
Karthik Rajendiran
Ranch Hand

Joined: Aug 13, 2004
Posts: 211
Thanks to all who replied.

1. Which one would be performance effective usually.

First i thought of using a String with
${0},${1} in parameter places
like
<emp:getEmployeeDetails>
<id>${0}</id>
</emp:getEmployeeDetails>

Using MessageFormat api java, we can replace the argument with values of user.

2. generating client would be easy than doing this string buffer chunk in terms of performance. What do you guys say?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: httpclient for accessing webservice