Call web service in java

Discussion in 'Java' started by Harshit, Jan 6, 2016.

  1. Harshit

    Harshit

    Joined:
    Jan 6, 2016
    Messages:
    1
    Likes Received:
    0
    I am trying to make a program which sends data to web service but I am getting an error. While compiling, I am not getting error but only at runtime, I am getting the error.

    Code (Text):
    import java.util.Iterator;
    import javax.xml.soap.*;
    import javax.xml.transform.*;
    import javax.xml.transform.stream.StreamResult;

    import org.w3c.dom.Node;

    public class CallWebServiceAdd {

    public static void main(String[] args) {
            // TODO code application logic here
            String a = "10";
        String b = "20";

        String op = "Addweb";
        String urn = "WebService1";
        String dest = "http://delecc01.india.jcb.local:8000/sap/bc/srt/wsdl/srvc_002655F3F9821EE5ACEC62338B931F00/wsdl11/allinone/ws_policy/document?sap-client=101";

        try
        {
            SOAPConnectionFactory soapConnFact = SOAPConnectionFactory.newInstance();
            SOAPConnection conn = soapConnFact.createConnection();

            MessageFactory msgFact = MessageFactory.newInstance();
            SOAPMessage msg = msgFact.createMessage();

            String authorization = new sun.misc.BASE64Encoder().encode(("username:password").getBytes());

        SOAPPart soapPart = msg.getSOAPPart();
            SOAPEnvelope envelop = soapPart.getEnvelope();
            envelop.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
            SOAPBody body = envelop.getBody();

            SOAPElement bodyelement = body.addChildElement("Addition");
            SOAPElement bodyelement1 = bodyelement.addChildElement("a").addTextNode(a);
            SOAPElement bodyelement2 = bodyelement.addChildElement("b").addTextNode(b);

            MimeHeaders headers = msg.getMimeHeaders();
            headers.addHeader("SOAPAction", "#OP_IF_OP_ZTEST_ALL");
        headers.addHeader("Authorization", "Basic " + authorization);
            msg.writeTo(System.out);
            msg.saveChanges();

            SOAPMessage reply = conn.call(msg, dest);

            soapPart = reply.getSOAPPart();
            envelop = soapPart.getEnvelope();
            body = envelop.getBody();

            Iterator iter = body.getChildElements();
            Node resultOuter = ((Node)iter.next()).getFirstChild();
            Node result = resultOuter.getFirstChild();

            System.out.println("add(" + a + ","+ b + ") = " + result.getNodeValue());
            reply.writeTo(System.out);

            conn.close();
            }
            catch(Exception e)
            {
                System.out.println(e.getMessage());
            }


        }

    }
    Error

    Code (Text):
    Jan 06, 2
      016 3:27:07 PM com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl lookForEnve
      lope
      SEVERE: SAAJ0514: Unable to create envelope from given source because the root e
      lement is not named Envelope
      Jan 06, 2016 3:27:07 PM com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory
      createEnvelope
      SEVERE: SAAJ0511: Unable to create envelope from given source
      Unable to create envelope from given source:
    Why this error is coming ?
     
    Harshit, Jan 6, 2016
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. bill sandner
    Replies:
    1
    Views:
    1,157
    iksrazal
    Jul 1, 2004
  2. hocho888
    Replies:
    1
    Views:
    966
    iksrazal@terra.com.br
    Apr 29, 2005
  3. Michael Averstegge
    Replies:
    0
    Views:
    4,641
    Michael Averstegge
    Jan 10, 2006
  4. Not Me
    Replies:
    1
    Views:
    3,615
    Not Me
    Jun 5, 2007
  5. John Straumann

    Call .NET web service from Java

    John Straumann, May 3, 2008, in forum: Java
    Replies:
    4
    Views:
    1,006
    John Straumann
    May 5, 2008
  6. RJ
    Replies:
    4
    Views:
    1,105
    Brock Allen
    Sep 22, 2005
  7. Leo Violette
    Replies:
    0
    Views:
    1,402
    Leo Violette
    Apr 17, 2009
  8. Redhot
    Replies:
    0
    Views:
    953
    Redhot
    Jul 18, 2009
Loading...