If you are looking for java client for RESTful web service then you should visit this article:
Simple REST client in java
This article will teach you how to create a SOAP client in java. That is creating a client in java which requests soap server (no need to be in java) and get response from it. First create request message as follows:
SOAPMessage message = MessageFactory.newInstance().createMessage(); SOAPHeader header = message.getSOAPHeader(); header.detachNode();
If you have namespace required in SOAP request then add those namespaces to envelope element as follows:
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); envelope.setAttribute("namspace","namespaceUrl");
You can add as many attribute as you want. Now time to create request message body.
Following body is made assuming that SOAP server where this client will connect will have a public service method called getResponse(name) available.
SOAPBody body = message.getSOAPBody(); QName bodyName = new QName("getResponse"); SOAPBodyElement bodyElement = body.addBodyElement(bodyName); SOAPElement symbol = bodyElement.addChildElement("name"); symbol.addTextNode(“Harry joy”);
Now that request message is ready it’s time to connect to soap server and send request. Following code will do this:
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection(); SOAPMessage response = connection.call(message, endpoint); connection.close();
In above code endpoint is the SOAP server URL without “?wsdl”. To parse response you can do as follows:
SOAPBody responseBody = response.getSOAPBody(); SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next(); SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next(); if(responseBody.getFault() != null) { //-- If response has any fault. System.out.println(returnElement.getValue()+" "+responseBody.getFault().getFaultString()); } else { System.out.println(returnElement.getValue()); }
Here request/response messages are totally dependent on SOAP server, how you have configured it.
Bonus: How to print request/response xml?
String getXmlFromSOAPMessage(SOAPMessage msg) throws SOAPException, IOException { ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); msg.writeTo(byteArrayOS); return new String(byteArrayOS.toByteArray()); }
Use:
System.out.println(getXmlFromSOAPMessage(message)); System.out.println(getXmlFromSOAPMessage(response));
Full source code:
public class SOAPClient { private static final String endpoint = "http://localhost/SOAPService/MySoapService"; public static void main(String[] args) throws SOAPException { SOAPMessage message = MessageFactory.newInstance().createMessage(); SOAPHeader header = message.getSOAPHeader(); header.detachNode(); SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); envelope.setAttribute("namespace","namespaceUrl"); SOAPBody body = message.getSOAPBody(); QName bodyName = new QName("getResponse"); SOAPBodyElement bodyElement = body.addBodyElement(bodyName); SOAPElement symbol = bodyElement.addChildElement("name"); symbol.addTextNode("Harry Joy"); SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection(); SOAPMessage response = connection.call(message, endpoint); connection.close(); SOAPBody responseBody = response.getSOAPBody(); SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next(); SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next(); if(responseBody.getFault()!=null){ System.out.println(returnElement.getValue()+" "+responseBody.getFault().getFaultString()); } else { System.out.println(returnElement.getValue()); } try { System.out.println(getXmlFromSOAPMessage(message)); System.out.println(getXmlFromSOAPMessage(response)); } catch (IOException e) { e.printStackTrace(); } } private static String getXmlFromSOAPMessage(SOAPMessage msg) throws SOAPException, IOException { ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); msg.writeTo(byteArrayOS); return new String(byteArrayOS.toByteArray()); } }
Hi… is there a way to use it with NTLM? Thanks in advance
Hi Marcelo,
Sorry but I have never worked with NTLM, so can not make a statement here. But after a little search on internet I found that this is possible, you should try SOAP-UI.
Thanks & Regards.
jdk1.6 and php soap client : http://biclim.com/WSClients.action
I have a problem when connect with this code because the webservice return the message with conten-type application/xml and trigger a Exception “Invalid conten-type: application/xml. Is this an error mesage instead of a SOAP Response? ” You know how to resolve this??
Hi Eduardo,
I tried this code with a web service generating XML response and worked perfect. You should check the request XML you are sending is valid at server side. It can be possible that either you are not sending any content or too long contents or content you are sending in request is not in valid format for the server. If you want me to debug the issue then you have to send web service address and other associated details run this program on my personal email id, if its feasible for you. You can find my email id on about page. I’ll still try to see if I can reproduce this error and let you know the update. Also telling line on which you are getting this exception will be a good help to debug it.
Hi Harry,
I used your code to try to consume the following service:
http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl
The message that is being sent is the following:
192.168.1.2
whereas the message sent if I use SoapUI is the following:
192.168.1.2
apparently it looks to me like the problem is because IPAddress and GetGeoIP are not attributed to web ns. Am I right? If yes, how can I set them to follow the web ns?
The error I’m getting is the following:
Unable to handle request without a valid action parameter. Please supply a valid soap action.
Thank you for any help!
Jad
N.B: this post contains the messages!
Hi Harry,
I used your code to try to consume the following service:
http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl
The message that is being sent is the following:
<!–
192.168.1.2
–>
whereas the message sent if I use SoapUI is the following:
<!–
192.168.1.2
–>
apparently it looks to me like the problem is because IPAddress and GetGeoIP are not attributed to web ns. Am I right? If yes, how can I set them to follow the web ns?
The error I’m getting is the following:
Unable to handle request without a valid action parameter. Please supply a valid soap action.
Thank you for any help!
Jad
N.B: this time this post really contains the messages! You can delete the 2 other posts!
Hi Harry,
I used your code to try to consume the following service:
http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl
The message that is being sent is the following:
whereas the message sent if I use SoapUI is the following:
apparently it looks to me like the problem is because IPAddress and GetGeoIP are not attributed to web ns. Am I right? If yes, how can I set them to follow the web ns?
The error I’m getting is the following:
Unable to handle request without a valid action parameter. Please supply a valid soap action.
Thank you for any help!
Jad
Finally the following code worked very well:
SEI UN GRANDEEEEEEEEE!!!!!!!!!
hi I am getting [ Unable to handle request without a valid action parameter. Please supply a valid soap action. ] error when I use your code with endpoint = “http://www.webservicex.net/country.asmx?WSDL” , QName bodyName = new QName(“GetCurrencyByCountry”); , SOAPElement symbol = bodyElement.addChildElement(“CountryName”);
symbol.addTextNode(“Singapore”); I want make a sample call to http://www.webservicex.net/country.asmx?op=GetCurrencyByCountry . Can you help me out?
Hi kitokid,
If you look at the link in your comment, they have displayed sample soap request as follows:
In this request they have used namespace uri for GetCurrencyByContry tag, which you are missing in your code. Add the namespace uri as follows and your code will work properly.
You can also add namespaces defined in envelope element as follows:
Hope this helps you.
Have a good day !! 🙂
Being a novice with Java, and required to call a Web Service with very limited time to implement a solution, your post has proven to be BY FAR the most helpful bit of information I have come across in the last 20 hours of research. Thank you!
Hi I am new to soap,Igot
“Unable to create message factory for SOAP: org.jboss.ws.core.soap.MessageFactoryImpl cannot be cast to javax.xml.soap.MessageFactory “—-
if anyone knows plz tell me………..
Hi I am new to soap,Igot
“Unable to create message factory for SOAP: org.jboss.ws.core.soap.MessageFactoryImpl cannot be cast to javax.xml.soap.MessageFactory “—-
if anyone knows plz tell me………..
Hi Jyothsna,
It seems that you have a classloader conflict, please check jar files in your application lib folder.
Regards.
@harryjoy..
Thanks a ton for this wonderful post. This has immensely helped me(for me being a novice).
I have following questions. Could you please help me with this?
1. If an EndPoint(A wsdl url) is given, Is there a way I can populate the available requests in Java? (This feature is available in SOAP UI. If you provide wsdl url, it populates requests)
2. Can you please post your above post for this sample Web service available in this link. http://www.webservicemart.com/uszip.asmx?WSDL.
I was not able to create the SOAPBody and Got stock there…
Thank you again..
MKrishna