此函数是否会在多线程条件下导致死锁?这段代码的问题是什么?
此代码尝试通过给定代理连接网站,以确定此代理是否可用(如果可用,响应代码应为200)。
但是这个功能会在一段时间内从不返回。
public static boolean verifyProxy(String ip, int port ){
boolean useful;
try {
URL url = new URL(VERIFY_URL);
InetSocketAddress addr = new InetSocketAddress(ip, port);
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
connection.setConnectTimeout(4 * 1000);
int rCode = connection.getResponseCode();
connection.disconnect();
useful = rCode == 200;
} catch (IOException e1){
logger.warn(String.format("verify proxy %s:%d exception: "+e1.getMessage(),ip, port));
useful = false;
}
logger.info(String.format("verify proxy %s:%d useful: "+useful, ip, port));
return useful;
}