Sub SetProxy (Host As String, Port As Int, Username As String, Password As String)
If HttpUtils2Service.hc.IsInitialized = False Then
HttpUtils2Service.Service_Create
End If
Dim jo As JavaObject = HttpUtils2Service.hc
Dim proxy, socketAddress As JavaObject
socketAddress.InitializeNewInstance("java.net.InetSocketAddress", Array (Host, Port))
proxy.InitializeNewInstance("java.net.Proxy", Array ("HTTP", socketAddress))
Dim client As JavaObject = jo.RunMethod("sharedInit", Array("hc"))
client.RunMethod("proxy", Array(proxy))
Dim pa As JavaObject
pa.InitializeNewInstance(GetType(Me) & "$MyProxyAuthenticator", Array(Username, Password))
client.RunMethod("proxyAuthenticator", Array(pa))
jo.SetField("client", client.RunMethod("build", Null))
End Sub
#if Java
public static class MyProxyAuthenticator implements okhttp3.Authenticator {
private final String username, password;
public MyProxyAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public okhttp3.Request authenticate(okhttp3.Route route, okhttp3.Response response) throws java.io.IOException {
String credential = okhttp3.Credentials.basic(username, password);
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
}
#End If