1

I want to get Response from request of SOAP URL. For this I am using below code

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;

public class Test {
    final static String UserLogin = "tuser01@xyz.com";

    public static void main(String[] args) {
        try {
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();
            String url = "http://devlocal04:8080/arsys/WSDL/public/devlocal03/DAL:OrderShim_WS";
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
            printSOAPResponse(soapResponse);
            soapConnection.close();
        } catch (Exception e) {
            System.out.println("Exception : " + e);
        }
    }

    private static SOAPMessage createSOAPRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();
        String serverURI = "urn:DAL:OrderShim_WS";
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("urn", serverURI);

        SOAPHeader soapHeader = envelope.getHeader();
        SOAPElement soapHeaderElem = soapHeader.addChildElement("AuthenticationInfo", "urn");
        SOAPElement soapHeaderElem1 = soapHeaderElem.addChildElement("userName", "urn");
        soapHeaderElem1.addTextNode("Test");
        SOAPElement soapHeaderElem2 = soapHeaderElem.addChildElement("password", "urn");
        soapHeaderElem2.addTextNode("12345");
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("Get_Order", "urn");
        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("Login_Name", "urn");
        soapBodyElem1.addTextNode(UserLogin);
        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", serverURI + "Login_Name");

        soapMessage.saveChanges();
        System.out.print("Request SOAP Message = ");
        soapMessage.writeTo(System.out);
        System.out.println();
        return soapMessage;
    }

    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        System.out.print("\nResponse SOAP Message = ");
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
    }
}

I have to do Request like below

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:DAL:OrderShim_WS">
   <soapenv:Header>
      <urn:AuthenticationInfo>
         <urn:userName>Test</urn:userName>
         <urn:password>12345</urn:password>
      </urn:AuthenticationInfo>
   </soapenv:Header>
   <soapenv:Body>
      <urn:Get_Order>
         <urn:Login_Name>tuser01@xyz.com</urn:Login_Name>
      </urn:Get_Order>
   </soapenv:Body>
</soapenv:Envelope>

But when I run my code they call this SOAP by using below Request

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:DAL:OrderShim_WS">
    <SOAP-ENV:Header>
        <urn:AuthenticationInfo>
            <urn:userName>Test</urn:userName>
            <urn:password>12345</urn:password>
        </urn:AuthenticationInfo>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <urn:Get_Order>
            <urn:Login_Name>tuser01@xyz.com</urn:Login_Name>
        </urn:Get_Order>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

In my request I can see "SOAP-ENV" in plcae of "soapenv" and I am not getting the valid response.

I am using Java 1.8.

user3441151
  • 1,880
  • 6
  • 35
  • 79
  • SOAP-ENV is the default prefix used by Spring-WS See https://stackoverflow.com/questions/39242197/how-to-change-soap-env-default-prefix-of-spring-ws – sercasti May 29 '17 at 15:09

1 Answers1

2

I think you are getting this error because of SOAP-ENV.Replace SOAP-ENV with soapenv or make your request same as the required.You will get an idea from my code

MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = "";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        soapMessage
                .getSOAPPart()
                .getEnvelope()
                .removeAttributeNS("http://schemas.xmlsoap.org/soap/envelope/",
                        "SOAP-ENV");
        soapMessage.getSOAPPart().getEnvelope()
                .removeAttribute("xmlns:SOAP-ENV");
        soapMessage.getSOAPHeader().detachNode();
        soapMessage.getSOAPPart().getEnvelope().setPrefix("soap");
        soapMessage.getSOAPBody().setPrefix("soap");

// envelope.addNamespaceDeclaration("ns1", serverURI);
// we need to send the soap request in the format mentioned below
/*
 * xmlInput =
 * "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n"
 * + "    <soap:Body>\r\n" +
 * "        <ns1:LoginProcesRequest xmlns:ns1=\"http://xmlns.oracle.com/GetSFDCDetailsProcess\">\r\n"
 * + "           <ns1:Login>1000</ns1:Login>\r\n" +
 * "           <ns1:Company>1000</ns1:Company>\r\n" +
 * "        </ns1:LoginProcesRequest>\r\n" + "    </soap:Body>\r\n" +
 * "</soap:Envelope>";
 */

// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement(
        "LoginProcesRequest", "ns1", serverURI);
SOAPElement soapBodyElem1 = soapBodyElem
        .addChildElement("Login", "ns1");
soapBodyElem1.addTextNode(inputlogin);
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("Company",
        "ns1");
soapBodyElem2.addTextNode(inputcompany);
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "LoginProcesRequest");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();

You can explore this easily

Vikram Saini
  • 2,713
  • 1
  • 16
  • 33
  • Now I can POST exactly what I needed. But I am not able to get Valid Response. I am getting the whole WSDL in Response. – user3441151 May 30 '17 at 18:29
  • yes your code looks fine to me.try to hit your wsdl in browser directly by removing ?wsdl from wsdl url.you will see the xml request which you have to hit.Convert your request to same format and then run your code – Vikram Saini May 30 '17 at 18:48
  • Now I can get Response ` OrdNumber0011556 ` Can you please help me to get `OrderNumber` from this Response. – user3441151 May 30 '17 at 18:59
  • make sure you write an exact xpath expression `DocumentBuilderFactory docFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(new InputSource(new StringReader( response))); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); string ordernumber= (String) xpath .evaluate( "/Envelope/Body/Get_Ord‌​erResponse/OrderNumber", doc, XPathConstants.STRING);` – Vikram Saini May 30 '17 at 19:05
  • convert your response in string and then pass it to docbuilder `Source sourceContent = soapResponse.getSOAPPart().getContent(); transformer.transform(sourceContent, new StreamResult(out)); String response = out.toString();` – Vikram Saini May 30 '17 at 19:09
  • did you get yur answer? – Vikram Saini Jun 03 '17 at 08:45
  • Yes. I got my answer. – user3441151 Jun 05 '17 at 10:03
  • This is not the correct answer. But its useful for me So I accept it as Useful. – user3441151 Jun 05 '17 at 10:35