6

我正在尝试对 SOCKS 代理列表进行排序,并找出哪些代理的连接和读取时间小于 1000 毫秒,这是我的代码

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.");
            }
        }

但是他们每个人都通过了测试,即使我TIMEOUT_VALUE = 1;做了我做错了什么?谢谢你的帮助。

4

2 回答 2

13

我认为您的问题是您没有从连接中读取任何内容。如果我设置TIMEOUT_VALUE得太低,我会得到一个例外。无论我阅读所有响应还是仅一行都不会影响结果时间,我想这是因为我在一个数据包中得到了完整的答案。

这是我使用的测量值(没有代理):

    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.");
    }
于 2011-06-30T09:33:25.640 回答
0

我用

    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");
于 2014-08-27T21:34:31.443 回答