HTTP 또는 Socks 프록시인지 테스트 할 프록시 목록이 있지만 connection.getContent() 또는 connection.getInputStream()을 호출하면 Java 코드가 중단됩니다. 이 문제는 프록시 서버가 응답하지 않고 서버에서 응답을 기다리는 코드 블록이 발생할 때 발생합니다. 서버가 응답하지 않을 때이 코드가 멈추거나 막히는 것을 막을 수있어서 다음 프록시를 확인할 수 있습니다.이 Java 코드가 걸리지 않도록 방지하는 방법
import java.io.*;
import java.net.*;
import java.util.*;
public class ProxyTest {
public static void main(String[] args) throws IOException {
InetSocketAddress proxyAddress = new InetSocketAddress("myproxyaddress", 1234);
Proxy.Type proxyType = detectProxyType(proxyAddress);
}
public static Proxy.Type detectProxyType(InetSocketAddress proxyAddress) throws IOException {
URL url = new URL("http://www.google.com/");
List<Proxy.Type> proxyTypesToTry = Arrays.asList(Proxy.Type.SOCKS, Proxy.Type.HTTP);
for(Proxy.Type proxyType : proxyTypesToTry) {
Proxy proxy = new Proxy(proxyType, proxyAddress);
URLConnection connection = null;
try {
connection = url.openConnection(proxy);
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
connection.getContent();
//connection.getInputStream();
return(proxyType);
}
catch (SocketException e) {
e.printStackTrace();
}
}
return(null);
}
}
나는 이것을 시도했지만 스레드 t는 결코 멈추지 않을 것이고/끝내면 connection.getContent()가 호출 될 때 멈출 것이다. – John
+1 제안에 대한 스레드. 이것은 nonblockign (적어도 주 스레드에서) 요청을하는 방법입니다. @john 어떤 간격으로 Thread.interupt()를 호출하고 스레드가 자체를 중지해야하는지 여부를 확인하도록 할 수 있습니다. – hsanders