Created
November 4, 2011 20:46
-
-
Save virtix/1340440 to your computer and use it in GitHub Desktop.
Simple HttpClient 4.1 that performs an HTTP GET request through NTLM v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.util.List; | |
import java.util.ArrayList; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpHost; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.auth.AuthScope; | |
import org.apache.http.auth.NTCredentials; | |
import org.apache.http.auth.params.AuthPNames; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.params.AuthPolicy; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.protocol.BasicHttpContext; | |
import org.apache.http.protocol.HttpContext; | |
import org.apache.http.util.EntityUtils; | |
import org.junit.Test; | |
public class TestSimpleHttpNTLMConnection { | |
@Test | |
public void testConnection() throws ClientProtocolException, IOException { | |
DefaultHttpClient httpclient = new DefaultHttpClient(); | |
List<String> authpref = new ArrayList<String>(); | |
authpref.add(AuthPolicy.NTLM); | |
httpclient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authpref); | |
NTCredentials creds = new NTCredentials("username", "pwd", "localhost", "domain"); | |
httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds); | |
HttpHost target = new HttpHost("localhost", 80, "http"); | |
// Make sure the same context is used to execute logically related requests | |
HttpContext localContext = new BasicHttpContext(); | |
// Execute a cheap method first. This will trigger NTLM authentication | |
HttpGet httpget = new HttpGet("/index.html"); | |
HttpResponse response = httpclient.execute(target, httpget, localContext); | |
HttpEntity entity = response.getEntity(); | |
System.out.println(EntityUtils.toString(entity)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, Thanks you for the client code. I have few questions related to NTLM,
How can i configure NTLM Authentication set up using Windows 7, Is it Possible ?
Or Is There any other Tool with which i can configure NTLM in my windows ?
I have followed http://developers.de/blogs/damir_dobric/archive/2009/08/16/enabling-of-ntlm-on-windows-7-and-windows-server-2008-r2.aspx But failed to achieve the authentication.
Please help