Search code examples
javaapache-httpclient-4.xntlm

Apache HttpClient - NTLM : 500 Internal Server Error


I'm using Apache HttpClient version 4.5.3 to get some piece of information from a webservice (Microsoft-IIS/8.5).

Therfore NTLM authentication is required. But I'm getting 500 Internal Server Error at the end of this process although everything seems working well (sending messagetype1, receiving challenge, sending messagetype3).

Below the code and thanks if you could give some help to solve this error.

String workstation = "someurl.com";
String domain ="DOMBIN";
String userName = "KSY5989";

String soapMessage ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"https://tempuri.org/\">"+
        "<soapenv:Header/>"+ 
        "<soapenv:Body>"+
        "<tem:GetPass>"+
            "<tem:PasswordWSRequest>"+
                "<tem:UserName>user658</tem:UserName>"+
                "<tem:Address>someurl.com</tem:Address>"+
                "<tem:ConnectionTimeout>60</tem:ConnectionTimeout>"+
            "</tem:PasswordWSRequest>"+
        "</tem:GetPass>"+
        "</soapenv:Body>"+
        "</soapenv:Envelope>";


SSLContext sslContext = SSLContexts
         .custom()
         //FIXME to contain real trust store
         .loadTrustMaterial(new TrustStrategy() {
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                // TODO Auto-generated method stub
                return true;
            }

         })
         .build();

Registry<AuthSchemeProvider> authSchemeRegistry2 = RegistryBuilder.<AuthSchemeProvider>create()
        .register(AuthSchemes.NTLM, new NTLMSchemeFactory())
        .build();

NTCredentials credentials = new NTCredentials(userName, password, workstation, domain);

CredentialsProvider credsProvider = new BasicCredentialsProvider();             
credsProvider.setCredentials(AuthScope.ANY, credentials);

try(CloseableHttpClient httpClient = HttpClients.custom().setDefaultAuthSchemeRegistry(authSchemeRegistry2).setSSLContext(sslContext).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build()){

    HttpHost target = new HttpHost("someurl.net", 5489, "https");

    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);

    HttpPost httppost = new HttpPost("/TRWebService/v1.1/trib.asmx");

    httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
    httppost.setHeader("SOAPAction", "https://tempuri.org/GetPassword");

    StringEntity stringEntity = new StringEntity(soapMessage, "UTF-8");
    stringEntity.setChunked(true);
    httppost.setEntity(stringEntity);

    CloseableHttpResponse response = httpClient.execute(target, httppost, context);
}

Solution

  • problem solved thanks to SaopUI which directed me to the cause of this problem.