package tk.yourchanges.clicker.proxy;
import java.util.Properties;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.IncorrectnessListener;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
public class Proxy {
public String host;
public int port;
private Properties properties;
public Proxy(final String host, final int port, final Properties properties) {
this.host = host;
this.port = port;
this.properties= properties;
}
/**
* 使用指定的 URL 验证代理.
*
* @param url 指定的 URL
* @return
*/
public boolean validate(final String url) {
try {
final WebClient webClient = new WebClient(BrowserVersion.CHROME, host, port);
webClient.setIncorrectnessListener(new IncorrectnessListener() {
@Override
public void notify(String arg0, Object arg1) {
}
});
String proxyValidationTimeout =properties.getProperty("validateProxyTimeout", "1000");
webClient.getOptions().setTimeout(Integer.valueOf(proxyValidationTimeout));
webClient.getOptions().setActiveXNative(false);
webClient.getOptions().setAppletEnabled(false);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setJavaScriptEnabled(false);
final Page page = webClient.getPage(url);
return 200 == page.getWebResponse().getStatusCode();
} catch (final Exception e) {
}
return false;
}
@Override
public String toString() {
return host + ":" + port;
}
}