Working with a Backconnect proxy service (Oxylab.io) we spent a long time looking for a way to authorize it. Originally we used JSoup to get the web pages’ content. The proxy() method can be used there when setting up the connection, yet it only accepts the host and port, no authentication is possible. One of the options that we found, was the following:
import java.net.Authenticator;
class ProxyAuthenticator extends Authenticator {
private String user, password;
public ProxyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password.toCharArray());
}
}
This option worked for us, but not always. When testing thru https://ipinfo.io we got the proxy working, but when testing at other sites the proxy didn’t work as intended. While trying to set proxy authorization for Oxylab.io in the code, we found the following useful code in Oxylab’s documentation:
import org.apache.http.HttpHost;
import org.apache.http.client.fluent.*;
public class Example {
public static void main(String[] args) throws Exception {
String url = "https://ipinfo.io/ip";
String host = "pr.oxylabs.io";
String port = 7777;
String login = "xxxx";
String password = "xxxx";
HttpHost entry = new HttpHost(host, port);
String query = Executor.newInstance()
.auth(entry, login, password).execute(Request.Get(url).viaProxy(entry))
.returnContent().asString();
System.out.println(query);
}
}
Class HttpHost, from the documentation:
The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.
Source: Oxylab documentation.