import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpHost; import org.apache.commons.httpclient.HttpState; import org.apache.commons.httpclient.NTCredentials; import org.apache.commons.httpclient.ProxyHost; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.protocol.Protocol; public class TestNTLMAuth { // ------------------------------------------------------------------- Main public static void main(String args[]) { try { test(); } catch (Exception e) { System.out.println(e.getMessage()); } } /** * Make sure preemptive authorization works when the server requires NLM. * @throws Exception */ public static void test() throws Exception { HttpClient client = new HttpClient(); ProxyHost proxyHost = new ProxyHost("your proxy host", 80); client.getHostConfiguration().setProxyHost(proxyHost); client.getHostConfiguration().setHost("www.switch.ch", 80, Protocol.getProtocol("http")); NTCredentials creds = new NTCredentials("user", "passord", "your host", "NT domain"); HttpState state = new HttpState(); // state.setCredentials(AuthScope.ANY, creds); state.setProxyCredentials(AuthScope.ANY, creds); client.setState(state); GetMethod httpget = new GetMethod("/index.html"); try { client.executeMethod(httpget); System.out.println(httpget.getResponseBodyAsString()); } finally { httpget.releaseConnection(); } } }