I'm trying to use the Apache HttpClient 4.1.1 library (http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html) to access sites from behind my company's proxy that uses the ISA Server with NTLM authentication but I keep getting an HTTP 407 Proxy Authentication Required error:
Code Snippet
HttpHost proxy = new HttpHost("myProxyHost", 80, "http");
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
NTCredentials creds = new NTCredentials("myWindowsUserName", "myWindowsPwd", "localhost", "myCompanyDomain");
AuthScope authScope = new AuthScope("myProxyHost", 80, "", "NTLM");
httpClient.getCredentialsProvider().setCredentials(authScope, creds);
HttpHost target = new HttpHost("www.google.com", 80, "http");
HttpGet get = new HttpGet("/");
System.out.println("executing request to " + target + " via " + proxy);
HttpResponse rsp = httpClient.execute(target, get);
System.out.println("----------------------------------------");
System.out.println(rsp.getStatusLine());
Header[] headers = rsp.getAllHeaders();
for (int i = 0; i<headers.length; i++) {
System.out.println(headers[i]);
}
System.out.println("----------------------------------------");
O/P
executing request to http://www.google.com:80 via http://myProxyHost:80 ---------------------------------------- HTTP/1.1 407 Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied. ) Via: 1.1 myCompanyServer Proxy-Authenticate: Negotiate Proxy-Authenticate: Kerberos Proxy-Authenticate: NTLM Connection: Keep-Alive Proxy-Connection: Keep-Alive Pragma: no-cache Cache-Control: no-cache Content-Type: text/html Content-Length: 4120 ----------------------------------------
What am I missing here?
Update: In the same environment, code using the JDK URL and URLConnection classes works!
Working Code Snippet
System.setProperty("http.proxyHost", "myProxyHost");
System.setProperty("http.proxyPort", "80");
URL url = new URL("http://www.google.com");
URLConnection con = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
O/P
Google window.google={kEI:"_N3cTaLFMY6cvgOH9MypDw",...