0

Hi I'm trying to call a SOAP Api from java, which tries to connect using tenantid, username, password, roles.

package com.virima.test;

import javax.xml.soap.*;

public class SOAPClient {

    public static void main(String args[]) {
        String soapEndpointUrl = "https://virimadev.trysaasit.com/ServiceAPI/FRSHEATIntegration.asmx";
        String soapAction = "SaaS.Services/Connect";

        callSoapWebService(soapEndpointUrl, soapAction);
    }

    private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String myNamespace = "ns";
        String myNamespaceURI = "SaaS.Services";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("tenantId", myNamespace);
        soapBodyElem.addTextNode("virimadev.trysaasit.com");
        SOAPElement soapBodyElem1 = soapBody.addChildElement("userName", myNamespace);
        soapBodyElem1.addTextNode("Alan.Taylor");
        SOAPElement soapBodyElem2 = soapBody.addChildElement("userName", myNamespace);
        soapBodyElem2.addTextNode("password");
        SOAPElement soapBodyElem3 = soapBody.addChildElement("role", myNamespace);
        soapBodyElem3.addTextNode("Admin");
    }

    private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
        try {
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);

            // Print the SOAP Response
            System.out.println("Response SOAP Message:");
            soapResponse.writeTo(System.out);
            System.out.println();

            soapConnection.close();
        } catch (Exception e) {
            System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
            e.printStackTrace();
        }
    }

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

        createSoapEnvelope(soapMessage);

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", soapAction);

        soapMessage.saveChanges();

        /* Print the request message, just for debugging purposes */
        System.out.println("Request SOAP Message:");
        soapMessage.writeTo(System.out);
        System.out.println("\n");

        return soapMessage;
    }

}

But I'm getting the following error

Request SOAP Message:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="SaaS.Services"><SOAP-ENV:Header/><SOAP-ENV:Body><ns:tenantId>https://virimadev.trysaasit.com</ns:tenantId><ns:userName>Alan.Taylor</ns:userName><ns:userName>Manage</ns:userName><ns:role>Admin</ns:role></SOAP-ENV:Body></SOAP-ENV:Envelope>

I'm geting response with following error

Unhandled system exception: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&amp;amp;gt; System.ArgumentNullException: CacheKey constructor tenantId-argument cannot be null-or-empty.
Parameter name: tenantId

I'm passing tenantId, but still it is saying parameter tenantId cant be null. Is there anything I'm missing.

8
  • Are you sure you have correct myNamespaceURI? "SaaS.Services" doesn't look like URI at all. Commented Oct 16, 2024 at 13:36
  • <s:schema elementFormDefault="qualified" targetNamespace="SaaS.Services"> this is what targetNameSpace I found from the WSDL url. If this is not the myNamespaceURI, where can I find it? Commented Oct 16, 2024 at 17:13
  • Look like you have to ask help from people maintaining that service. Commented Oct 16, 2024 at 17:23
  • One thing you can try is to replace soapBody.addChildElement("tenantId", myNamespace) with soapBody.addAttribute("tenantId", myNamespace) Commented Oct 16, 2024 at 17:26
  • Also having XSD for it may help. Commented Oct 16, 2024 at 17:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.