Calling a SOAP Web Service from X++

There are a few instances in our system where I needed to call a SOAP web service, but for difference reasons was not able to create a reference in the AOT and utilize a proxy.

In those instances a basic method I use for connecting is below.


public xml soapWsCall(XmlDocument _xml)
{
    COM                 objXHTTP;
    xml                 xmlDocumentResponse;
    url                 xmlUrl;
    str                 xmlError;

    #define.serverXmlHttp('MSXML2.ServerXMLHTTP')


    #define.xmlResolve(3000)
    #define.xmlConnect(3000)
    #define.xmlSend(3000)
    #define.xmlReceive(3000)

    interopPermission   interopPermission;
    ;

    xmlUrl          = 'https://yourwebserviceurl.com';


    interopPermission = new InteropPermission(InteropKind::ComInterop);
    InteropPermission.assert();

    objXHTTP = new COM(#serverxmlHttp);
    objXHTTP.settimeouts(#xmlResolve, #xmlConnect, #xmlSend, #xmlReceive);
    objXHTTP.Open("POST", xmlUrl, False);
    objXHTTP.setRequestHeader("Content-Type", "text/xml.");

    try
    {
        objXHTTP.send(_xml.xml());
        xmlDocumentResponse = objXHTTP.responseText();
    }
    catch(Exception::Error)
    {
        xmlDocumentResponse = "An error has occurred.";
        codeAccessPermission::revertAssert();
    }

    objXHTTP = null;

    codeAccessPermission::revertAssert();

    return xmlDocumentResponse;

}

And then to handle the results, something like this:


        // save the file
        xmlDocument.save(fileName);

        // send the xml to WS and store the results
        resultsText = this.soapWsCall(xmlDocument);
        xmlReturned.loadXml(resultsText);

        // save the results
        xmlReturned.save(resultsFile);