[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

Web Services

Hi All,

I am trying to develop a java code for invoking a WebServicem dynamically. I have the URL of the webservice and am also aware of the service name, port name and the operation name.
Could you provide with a sample java code for the same.

Regards,
Sambhava
0
mickey_lovely
Asked:
mickey_lovely
  • 2
  • 2
1 Solution
 
CEHJCommented:
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You can use DII or dynamic proxy or any other method:

http://https://bpcatalog.dev.java.net/ nonav/soa/standalone-client/ (various methods explained, how to write a stand-alone client for a web-service)

http://wsdlj2ee.blogspot.com/2005/09/wsdl-sample.html

http://www.computerworld.com/developmenttopics/development/story/0,10801,95204,00.html

http://www.perfectxml.com/articles/xml/soapguide.asp

http://aspn.activestate.com/ASPN/Mail/Message/Apache-Soap-Users/736323

I would recommend you download Axis (http://ws.apache.org/axis/) and using the WSDL 2 Java tool, make a proxy class of the web-service. Then import it in your code and call it like any normal class/ method. It will handle calling the web-service internally for you.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0
 
Leo EikelmanCommented:
here is a document of code I created.  This connects to the web services, uses the method, and then parses through the XML.

Note:  The following code calls the getUserRole method from the web service and saves the XML to a file.
      
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import weblogic.knex.jdom.output.XMLOutputter;

public class SOAPClient
{
    public String getResponse()
    {
        String result = "";
       
        String m_portName = "PORT_NAME";
        String m_soapAddress = "LOCATION_OF_WEBSERVICES";
      String m_operationName = "METHOD_NAME"; // The method we are calling        
      String m_soapAction = "service=METHOD_NAME"; // binds the soap action to the method we are calling
        String m_portNamespaceURI = "PORT_NAMESPACE";
        String soapRequest;
               
        soapRequest = "<?xml version='1.0' encoding='utf-8'?>" +
                "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
                " xmlns:xsd='http://www.w3.org/2001/XMLSchema'" +
                " xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" +
                "<soap:Body>" +
                "<NAME_OF_THE_METHOD xmlns='NAMESPACE'>";

      /* The values are hardcoded below for this example */
                /* The information you are sending to the web service */
          soapRequest = soapRequest +
                "<strUserName>" + "mjing" + "" +
                "<strAppName>" + "*" + "" +
                "<strRoleName>" + "*" + "" +
                "</getUserRole>";
        try
        {
            URL url=new URL(m_soapAddress);
            HttpURLConnection httpCon = (HttpURLConnection)url.openConnection();
            httpCon.setAllowUserInteraction(false);
            httpCon.setDoOutput(true);
            httpCon.setDoInput(true);
            httpCon.setRequestProperty("Content-Type", "text/xml; charset=utf-8");  
            httpCon.setRequestProperty("SOAPAction", m_soapAction);
            httpCon.setRequestMethod("POST");
            OutputStream postStream = httpCon.getOutputStream();
            PrintWriter prnWriter = new PrintWriter(postStream);
            prnWriter.println(soapRequest);
            prnWriter.flush();
            prnWriter.close();            
           
            DocumentBuilderFactory factory =
                DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
           
            DocumentBuilder builder = factory.newDocumentBuilder();
            InputStream in = httpCon.getInputStream();
            Document document = builder.parse(in);
            in.close();
            httpCon.disconnect();
                                   
            NodeList products = document.getElementsByTagName("NAME OF THE ROOT NODE");
           
          /* Retrieves the first node, which holds the XML response */
            Node oneProduct = products.item(0);
           
          /* Retrieves the SOAP body, which is the XML returned */
            Text productValue = (Text) oneProduct.getFirstChild();
                       
            result = productValue.getNodeValue();
           
            FileOutputStream fout;
           
          /* Writes the xml to a file */
            fout = new FileOutputStream ("c:\\temp\\myfile.xml");
            new PrintStream(fout).print (result);
            fout.close();
        }

Cheers,
Leo
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now