Sub Service_Create
Dim jo As JavaObject = HttpUtils2Service.hc
Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))
Dim ntlm As JavaObject
ntlm.InitializeNewInstance(Application.PackageName & ".starter$NTLMAuthenticator", Array("username", "password", "domain", "workstation")) 'replace with your own values
builder.RunMethod("authenticator", Array(ntlm))
jo.SetField("client", builder.RunMethod("build", Null))
End Sub
#if JAVA
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.Proxy;
import java.util.List;
import org.apache.http.impl.auth.NTLMEngine;
import org.apache.http.impl.auth.NTLMEngineException;
import org.apache.http.impl.auth.NTLMScheme;
import okhttp3.Authenticator;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
public static class NTLMAuthenticator implements Authenticator {
final NTLMEngine engine;
private final String domain;
private final String username;
private final String password;
private final String ntlmMsg1;
private final String workstation;
public NTLMAuthenticator(String username, String password, String domain, String workstation) throws Exception {
this.domain = domain;
this.username = username;
this.workstation = workstation;
this.password = password;
NTLMScheme scheme = new NTLMScheme();
Field f = scheme.getClass().getDeclaredField("engine");
f.setAccessible(true);
engine = (NTLMEngine)f.get(scheme);
String localNtlmMsg1 = null;
localNtlmMsg1 = engine.generateType1Msg(domain, workstation);
ntlmMsg1 = localNtlmMsg1;
}
@Override
public Request authenticate(Route route, Response response) throws IOException {
final List<String> WWWAuthenticate = response.headers().values("WWW-Authenticate");
if (WWWAuthenticate.contains("NTLM")) {
return response.request().newBuilder().header("Authorization", "NTLM " + ntlmMsg1).build();
}
String ntlmMsg3 = null;
try {
ntlmMsg3 = engine.generateType3Msg(username, password, domain, workstation, WWWAuthenticate.get(0).substring(5));
} catch (NTLMEngineException e) {
throw new RuntimeException(e);
}
return response.request().newBuilder().header("Authorization", "NTLM " + ntlmMsg3).build();
}
}
#end if