I am trying to make a class to help me manage proxies in a program. I want to use apaches http client to check if the proxy is live and if it is get what time zone it is in. The class currently has dependencies on apaches http client and org.json. Whenever I try with an authenticated proxy it outputs this :
Checking proxy global.711proxy.com:10000
Proxy check failed: Remote host terminated the handshake
Proxy is dead or not in USA. Timezone: null
Here is my class. Everything else is working as intended.
import org.json.JSONObject;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.client5.http.classic.methods.HttpGet;
public class Proxy {
private String host;
private int port;
private String username;
private String password;
private boolean isAuth;
private boolean isLive;
private String timezone;
public Proxy(String proxyString) {
parseProxy(proxyString);
checkProxy();
}
private void parseProxy(String proxyString) {
String[] parts = proxyString.split(":");
if (parts.length < 2 || parts.length > 4) {
throw new IllegalArgumentException("Invalid proxy format. Use host:port or host:port:user:pass");
}
this.host = parts[0];
this.port = Integer.parseInt(parts[1]);
if (parts.length == 4) {
this.username = parts[2];
this.password = parts[3];
this.isAuth = true;
} else {
this.isAuth = false;
}
}
private void checkProxy() {
try {
System.out.println("Checking proxy " + host + ":" + port);
// Define target and proxy hosts
final HttpHost target = new HttpHost("https", "worldtimeapi.org", 443);
final HttpHost proxy = new HttpHost("http", host, port);
// Build client based on authentication needs
final CloseableHttpClient httpClient;
if (isAuth) {
// With authentication
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(host, port),
new UsernamePasswordCredentials(username, password.toCharArray())
);
httpClient = HttpClients.custom()
.setProxy(proxy)
.setDefaultCredentialsProvider(credsProvider)
.build();
} else {
// Without authentication
httpClient = HttpClients.custom()
.setProxy(proxy)
.build();
}
try {
final HttpGet request = new HttpGet("/api/ip");
httpClient.execute(target, request, response -> {
this.isLive = (response.getCode() >= 200 && response.getCode() < 400);
if (this.isLive) {
String jsonStr = EntityUtils.toString(response.getEntity());
JSONObject json = new JSONObject(jsonStr);
this.timezone = json.getString("timezone");
System.out.println("Proxy check successful. Timezone: " + timezone);
}
return null;
});
} finally {
httpClient.close();
}
} catch (Exception e) {
System.err.println("Proxy check failed: " + e.getMessage());
this.isLive = false;
this.timezone = null;
}
}
public String getHost() {
return host;
}
public int getPort() {
return port;
}
public boolean isAuth() {
return isAuth;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public boolean isLive() {
return isLive;
}
public String getTimezone() {
return timezone;
}
public void recheckLive() {
checkProxy();
}
@Override
public String toString() {
return String.format("Proxy{host='%s', port=%d, auth=%b, live=%b}",
host, port, isAuth, isLive);
}
}
curl "http://worldtimeapi.org/api/ip"