If a Java client application is running behind a proxy server, it is not able to connect with Internet services without special measures to get past the proxy. To work with a proxy, the Java client needs to specify information about the proxy itself, as well as user information for authentication on the proxy.
Configuring the proxy is done through a set of system properties in the Java VM:
-
http.proxySet
: indicates that a proxy server is used for outgoing connections. -
http.proxyHost
: defines the host name of the proxy server. -
http.proxyPort
: specifies what port the proxy is listening on.
These system properties can be configured programmatically through the java.lang.System
class:
System.getProperties().put("http.proxySet", "true");
System.getProperties().put("http.proxyHost", "proxy.mycompany.com");
System.getProperties().put("http.proxyPort", "8080");
Equivalently, these properties can also be configured through the VM parameters at startup:
java -Dhttp.proxySet=true -Dhttp.proxyHost=proxy.mycompany.com -Dhttp.proxyPort=8080
Authentication
If the proxy requires authentication, you can provide your credentials by using a custom ILcdOWSTransport
which sets the Proxy-Authorization
HTTP request property.
This approach is identical to the approach for connecting to a server which requires authentication.
ILcdOWSTransport transport = new TLcdOWSHttpTransport() {
@Override
protected void configureConnection(URI aURI, ILcdOWSRequest aRequest, HttpURLConnection aConnection) throws IOException {
super.configureConnection(aURI, aRequest, aConnection);
String credentials = "username:password";
String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes());
aConnection.setRequestProperty("Proxy-Authorization", "Basic " + encodedCredentials);
}
};