0

I have used javax.xml.soap library to invoke soap request and response. I have wsdl endpoint url which works fine in SOAP-UI. I need to validate the same using java, and I created a soap request which looks different evnelop than the actual one as below

JAVA REQUEST PAYLOAD:

   <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webservicex.com/">
     <SOAP-ENV:Header/>
       <SOAP-ENV:Body>
       <web:GetCitiesByCountry>
       <web:CountryName>US</web:CountryName>
       </web:GetCitiesByCountry>
     </SOAP-ENV:Body>
   </SOAP-ENV:Envelope>

SOAP REQUEST PAYLOAD:

   <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET">
     <soapenv:Header/>
       <soapenv:Body>
        <web:GetCitiesByCountry>
        <!--Optional:-->
        <web:CountryName>US</web:CountryName>
        </web:GetCitiesByCountry>
      </soapenv:Body>
</soapenv:Envelope>

The JAVA request payload is not working as expected and it throws below error response.

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://www.webservicex.com/GetCitiesByCountry.
   at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()
   at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
   at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>

I didn't find any difference between both request payload except the envelop, and I tried to modify the envelop by using

SOAPConstants.SOAP_1_1_PROTOCOL and 1_2 PROTOCOL but no luck.

The below code I tried so far:

    private static SOAPMessage createSOAPRequest() throws Exception 
     {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        //Tried to change the envelop which is not working
        /*MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
        SOAPMessage soapMessage =     messageFactory.createMessage();
        soapMessage.getSOAPPart().setPrefix("soapenv");
        soapMessage.getSOAPPart().removeMimeHeader("SOAP-ENV");
        soapMessage.getSOAPHeader().setPrefix("soapenv");
        soapMessage.getSOAPBody().setPrefix("soapenv");
        messageFactory.createMessage();*/
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = "http://www.webservicex.com/";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("web", serverURI);

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement(
                "GetCitiesByCountry", "web");
        SOAPElement soapBodyElem1 soapBodyElem.addChildElement("CountryName",
                "web");
        soapBodyElem1.addTextNode("US");

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", serverURI + "GetCitiesByCountry");

        System.out.println(soapMessage.getSOAPBody());
        soapMessage.saveChanges();

        /* Print the request message */
        System.out.print("Request SOAP Message:");
        soapMessage.writeTo(System.out);
        System.out.println();

        return soapMessage;
      }

I copied the request payload and change the envelop from SOAP-ENV to sopaenv and invoked in SOAPUI which is return an appropriate response. So can somebody tell me how can I change the envelop tag by using java library.

ArrchanaMohan
  • 2,314
  • 4
  • 36
  • 84

1 Answers1

0

can you try with
String serverURI = "http://www.webserviceX.NET"; instead of

String serverURI = "http://www.webservicex.com/";

harry
  • 310
  • 3
  • 15
  • I tried that one also. Please find the stack traces: _Server did not recognize the value of HTTP Header SOAPAction:http://www.webservicex.NET/GetCitiesByCountry._ – ArrchanaMohan Feb 12 '17 at 09:08
  • 1
    The issue is that java can't find the schema or xsd which contains the rules to deserialize the incoming stream of bytes to Java classes. The most straight forward way which works everytime is to setup a. Maven job which would take in the wsdl and generate Jaxb classes for you. – harry Feb 12 '17 at 15:23