Call Webservice With NTLM authentication (.NET Services)
Mirth Web Service sender and Http Sender connector types do not fully support NTLM Http Authentication scheme because it works by mantaining a persistent http connection to the server during authentication/negotiation.
This function can be used to call such services by using apache's HttpClient directly.
This function uses packages already included in Mirth (tested on 3.4.2)
Mirth Web Service sender and Http Sender connector types do not fully support NTLM Http Authentication scheme because it works by mantaining a persistent http connection to the server during authentication/negotiation.
This function can be used to call such services by using apache's HttpClient directly.
This function uses packages already included in Mirth (tested on 3.4.2)
Code:
var body = <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:pub="namespacex"> <soap:Header/> <soap:Body> <pub:query> input data </pub:query> </soap:Body> </soap:Envelope>; var endpoint = "/WSContext/aMicrosoftService.asmx"; var response = callWebServiceNtlm("servicehost", 80, "http", "user", "pass", "domain", body.toString(), endpoint); response.status;// holds the http status line response.responseBody;// holds the http response body as string function callWebServiceNtlm(host, port, protocol, username, password, domain, soapBody, endpoint) { importPackage(Packages.org.apache.http.impl.client); importPackage(Packages.org.apache.http); importPackage(Packages.org.apache.http.auth); importPackage(Packages.org.apache.http.client); importPackage(Packages.org.apache.http.client.methods); importPackage(Packages.org.apache.http.client.protocol); importPackage(Packages.org.apache.http.entity); importPackage(org.apache.commons.io); var hostName = 'unknown'; try{ hostName = Packages.java.net.InetAddress.getLocalHost().getHostName(); }catch(e){logger.error(e);} var httpclient = HttpClients.createDefault(); var credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(username, password, hostName, domain)); var target = new HttpHost(host, port, protocol); // Make sure the same context is used to execute logically related requests var context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); // Execute a cheap method first. This will trigger NTLM authentication var httppost = new HttpPost(endpoint); httppost.addHeader("Content-Type", "application/soap+xml"); httppost.setEntity(new StringEntity(soapBody)); var response1 = httpclient.execute(target, httppost, context); var log = {}; try { var entity1 = response1.getEntity(); log.status = response1.getStatusLine().toString(); var responseBody = IOUtils.toString(response1.getEntity().getContent(), "UTF-8"); log.responseBody = responseBody; } finally { response1.close(); } return log }
Comment