Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

java client code to send a  SOAP request  and read response from a WSDL url.

Posted on 2010-09-20
6
Medium Priority
?
1,062 Views
Last Modified: 2016-02-26
A SOAP webservice hosted on a server,
I know wsdl url and what tags to set in request.

Requirement:
 1) I want to set some fields(tags) of soap request.
  2) send the request
3) read the response.
Please point me to the java code to implement the above.

Thanks in Advance
0
Comment
Question by:srinivasp19
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
tom_sebastian earned 2000 total points
ID: 33719609
You can use spring-ws to do this task. You need jars for spring, spring-ws, axiom-api, axiom-impl etc..
Here is a working code using simple hardcoded xml request and prints the response xml. You can use marshaller for JaxB objects instead of plain xml string

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.springframework.ws.client.core.WebServiceTemplate;

public class WebServiceTest {

      /**
       * @param args
       */
      public static void main(String[] args) {
            System.err.println("Test");
            try {
                  String result = processRequest("test1","test2","test3");
                  System.err.println(result);
            } catch (Exception e) {
                  e.printStackTrace();
            }
     

      }

      public static String processRequest(String... parameters) throws Exception {
            WebServiceTemplate webService = new WebServiceTemplate();
            webService.setDefaultUri("<your endpoint url>");
            StringBuffer requestStr = new StringBuffer();
            requestStr.append("<YourRequest>");
            requestStr.append("<Field1>"+parameters[0]+"</Field1>");
            requestStr.append("<Field2>"+parameters[1]+"</Field2>");
            requestStr.append("<Field3>"+parameters[2]+"</Field3>");
            requestStr.append("</YourRequest>");
 
            Source requestPayload = new StreamSource(new ByteArrayInputStream(requestStr.toString().getBytes()));
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Result responseResult = new StreamResult(bos);
            webService.sendSourceAndReceiveToResult(requestPayload, responseResult);
            return bos.toString();
      }

}

0
 

Author Comment

by:srinivasp19
ID: 33724694
Hi  tom_sebastian,

Thank you for the reply.

I forgot to mention that the wsdl url is https.
How to add username and password for authentication in the SOAP request?.

I Prefer the Java code :
which takes WSDL URL with authentication as input  and get SOAP request xml, (similar to SOAPUI tool)
with the SOAP request xml object , I should set the required tag(paramenter of exposed service)  and  send request.

Thanks in advance.



 
0
 
LVL 3

Expert Comment

by:tom_sebastian
ID: 33726850
Set the username/password in the message sender, then add it to the soap header too
here is a sample code:

//Set the username/password in the message sender
WebServiceTemplate webService = new WebServiceTemplate();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("<username>","<password>");
CommonsHttpMessageSender msgSender = new CommonsHttpMessageSender();
msgSender.setCredentials(credentials);
webService.setMessageSender(msgSender);

......
.......

//add it to the soap header
webService.sendSourceAndReceiveToResult(requestPayload, new WSSEHeaderWebServiceMessageCallback("<username>","<password>"), responseResult);
0
 

Expert Comment

by:charlatanOfBelle
ID: 34255871
you can also Eclipse's webservice client wizard, just specify the wsdl and it does the rest, it auto-geneterates the interface/proxy that you will need to instantiate. For example it will generate a ServiceProxy which would contain the methods that the webservice host has exposed. Another helpful option when you use this, you can opt to generate sample jsp files that you can deploy and will show you how the ServicePRoxy is used. I've used this 3 times already in my career, the last one is for integration with Salesforce.

btw, It uses axis behind the scenes.

http://www.eclipse.org/webtools/community/tutorials/BottomUpAxis2WebService/bu_tutorial.html
0

Featured Post

Threat Trends for MSPs to Watch

See the findings.
Despite its humble beginnings, phishing has come a long way since those first crudely constructed emails. Today, phishing sites can appear and disappear in the length of a coffee break, and it takes more than a little know-how to keep your clients secure.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When coding a Webservice to provide data, the normal way is to create a method that returns a Dataset object.   But a Dataset cannot be used easily from another platform, such as PHP,  FLAS (FLEX) , etc. Solution : Return a XML Document instea…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question