November 2004
Intermediate to advanced
400 pages
10h 31m
English
You need to access a resource that is protected by Microsoft’s NTLM authentication protocol.
Create an instance of NTCredentials with a
username, password, host, and domain, and call
setCredentials()
on the HttpState
associated with an instance of HttpClient. The
following example demonstrates the use of
NTCredentials to access a resource on host
test.windowsmachine.com, which is on the domain
TESTDOM:
importorg.apache.commons.httpclient.Credentials;importorg.apache.commons.httpclient.HttpClient;importorg.apache.commons.httpclient.HttpState;importorg.apache.commons.httpclient.HttpException;importorg.apache.commons.httpclient.HttpMethod;importorg.apache.commons.httpclient.NTCredentials;importorg.apache.commons.httpclient.methods.GetMethod; HttpClient client =newHttpClient( ); // Set credentials on the client Credentials credentials = new NTCredentials( "testuser", "crazypass", "homecomputer ", "TESTDOM" ); HttpState state = client.getState( ); state( ).setCredentials( null, null, credentials ); String url = "http://webmail.domain.biz/exchange/"; HttpMethod method =newGetMethod( url ); client.executeMethod( method ); String response = method.getResponseBodyAsString( ); System.out.println( response ); method.releaseConnection( );
The parameters to the constructor of NTCredentials are the username, the password, a hostname, and a domain. The hostname is the name of the machine making the request, and, in this case, ...
Read now
Unlock full access