Finally,
Thanks for your answer phil.ho.
Yes this article was really helpful in a first place.
I've used to fetch some images or pages already using a custom classes in processing (or even with processing net library).
However, these tricks cannot be used within HttpClient.
So i go deeper in the documentation and found :
http://hc.apache.org/httpcomponents-client/ntlm.html
.
And now everything works.
So here's the tutorial for anyone who wants to send http/https' requests using HttpClient behind an HTTP proxy using NTLM authentification :
- Start a new sketch and save it.
- Download HttpClient & HttpCore here (HttpClient : binary with dependencies, HttpCore : binary – version 4.0.1 at post's date).
- Extract and place HttpClient, HttpCore & Commons logging jar files in the "code" folder next to your processing sketch (you can create that folder by hand or drag and dropping files in the processing editor window).
- Download Samba jcifs last version.
- Extract and place Jcifs jar file in the code folder created before.
- Create a new tab call it JCIFSEngine.java and paste the code below.
- Create another new tab call it NTLMSchemeFactory.java and paste the code below.
- Copy the code from the Template below in the sketch tab
- Tweak the parameters proxyHost, proxyPort, user, pwd, workstation, domain according to your network configuration.
- You can use the code in the bat file code provided in order to know your username, domain and workstation parameters.
- Now you're done.
JCIFSEngine.java
import jcifs.ntlmssp.Type1Message;
import jcifs.ntlmssp.Type2Message;
import jcifs.ntlmssp.Type3Message;
import jcifs.util.Base64;
import org.apache.http.impl.auth.NTLMEngine;
import org.apache.http.impl.auth.NTLMEngineException;
public class JCIFSEngine implements NTLMEngine {
public String generateType1Msg( String domain, String workstation) throws NTLMEngineException {
Type1Message t1m = new Type1Message(
Type1Message.getDefaultFlags(),
domain,
workstation);
return Base64.encode(t1m.toByteArray());
}
public String generateType3Msg(
String username,
String password,
String domain,
String workstation,
String challenge) throws NTLMEngineException {
Type2Message t2m;
try {
t2m = new Type2Message(Base64.decode(challenge));
}
catch (Exception ex) {
throw new NTLMEngineException("Invalid Type2 message", ex);
}
Type3Message t3m = new Type3Message(
t2m,
password,
domain,
username,
workstation,
Type3Message.getDefaultFlags());
return Base64.encode(t3m.toByteArray());
}
}
NTLMSchemeFactory.java
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthSchemeFactory;
import org.apache.http.impl.auth.NTLMScheme;
import org.apache.http.params.HttpParams;
public class NTLMSchemeFactory implements AuthSchemeFactory {
public AuthScheme newInstance(final HttpParams params) {
return new NTLMScheme(new JCIFSEngine());
}
}
Template
String proxyHost;
int proxyPort;
String user;
String pwd;
String workstation;
String domain;
// START_TWEAKING_BLOCK
// Init Proxy Parameters
proxyHost = "localhost";
proxyPort = 8080 ;
// NTLM Authentification parameters
user = "username";
pwd = "password";
workstation = "workstation.host.ext";
domain = "networkdomain";
// END_TWEAKING_BLOCK
HttpHost target = new HttpHost("www.processing.org", 80, "http");
HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http");
// general setup
SchemeRegistry supportedSchemes = new SchemeRegistry();
// Register the "http" and "https" protocol schemes, they are
// required by the default operator to look up socket factories.
supportedSchemes.register(new Scheme("http",
PlainSocketFactory.getSocketFactory(), 80));
supportedSchemes.register(new Scheme("https",
SSLSocketFactory.getSocketFactory(), 443));
// prepare parameters
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUseExpectContinue(params, true);
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params,
supportedSchemes);
DefaultHttpClient httpclient = new DefaultHttpClient(ccm, params);
httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(null, -1),
new NTCredentials(user, pwd, workstation, domain));
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpGet req = new HttpGet("/robots.txt");
System.out.println("executing request to " + target + " via " + proxy);
try {
HttpResponse rsp = httpclient.execute(target, req);
HttpEntity entity = rsp.getEntity();
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("----------------------------------------");
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
}
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
catch( Exception e ) {
e.printStackTrace();
}
GetSomeInfo.bat
set file="C:info.txt"
echo Domain: %USERDOMAIN% > %file%
echo Username: %USERNAME% >> %file%
echo Workstation: %COMPUTERNAME% >> %file%
echo OS: %OS% >> %file%
pause