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
mickey_lovelyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
0
Leo EikelmanDirector, IT and Business DevelopmentCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.