• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

httpclient for accessing webservice

 
Karthik Rajendiran
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Regards,
Andy
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic