I am trying to sort through a list of SOCKS proxies, and figure out which ones have a connect and read time of less than 1000ms, here is my code
for(Proxy p : proxies) {
try {
URLConnection testConnection = testUrl.openConnection(p);
testConnection.setConnectTimeout(TIMEOUT_VALUE);
testConnection.setReadTimeout(TIMEOUT_VALUE);
success.add(p);
} catch(SocketTimeoutException ste) {
System.out.println("Proxy " + p.address().toString() + " timed out.");
}
}
But every single one of them passes the test, even when I do TIMEOUT_VALUE = 1;
What am I doing wrong? Thanks for any help.
I assume your problem is you don't read anything from connection. If I set TIMEOUT_VALUE
too low, I get an exception. Whether I read all response or only one line did not affect the resulting time, I guess it is because I got whole answer in one packet.
Here is the measurement I used (without proxies):
int TIMEOUT_VALUE = 1000;
try {
URL testUrl = new URL("http://google.com");
StringBuilder answer = new StringBuilder(100000);
long start = System.nanoTime();
URLConnection testConnection = testUrl.openConnection();
testConnection.setConnectTimeout(TIMEOUT_VALUE);
testConnection.setReadTimeout(TIMEOUT_VALUE);
BufferedReader in = new BufferedReader(new InputStreamReader(testConnection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
answer.append(inputLine);
answer.append("\n");
}
in.close();
long elapsed = System.nanoTime() - start;
System.out.println("Elapsed (ms): " + elapsed / 1000000);
System.out.println("Answer:");
System.out.println(answer);
} catch (SocketTimeoutException e) {
System.out.println("More than " + TIMEOUT_VALUE + " elapsed.");
}
I use
System.getProperties().put("proxySet", "true");
System.getProperties().put("http.proxyHost", "192.168.10.121");
System.getProperties().put("http.proxyPort", "8080");
//System.getProperties().put("http.nonProxyHosts", "8080");