java - 如果服务器没有响应,超时不起作用

标签 java http timeout connection

我遇到的问题是,当服务器没有响应时,我永远不会收到 SocketTimeOutException。 30 秒后,我收到了 IOException。

当服务器没有响应时,我需要做什么才能超时?

这是我的代码,其中第一个 URL 起作用,第二个 URL 产生 IOException 而不是 SocketTimeOutException。

公共(public)类 TestConnection {

public static final int TIMEOUT_VALUE = 5000;

public static void main(String[] arg){

    try {
        URL testUrl = new URL("http://www.doublegames.com/images/games140/scrabble-cubes-online_140x140.jpg");
        testTimeOut(testUrl);
        testUrl = new URL("http://tamblang.co.cc/wp-content/uploads/2010/08/cd7ce2_command-038-conquer-4-tiberian-twilight-140x140.jpg");
        testTimeOut(testUrl);

    } catch (MalformedURLException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
}
private static void testTimeOut(URL testUrl) {
    try {
        long start = System.nanoTime();
        HttpURLConnection testConnection = (HttpURLConnection) testUrl.openConnection();
        testConnection.setConnectTimeout(TIMEOUT_VALUE);
        testConnection.setReadTimeout(TIMEOUT_VALUE);


        BufferedReader in = new BufferedReader(new InputStreamReader(testConnection.getInputStream()));
        long elapsed = System.nanoTime() - start;
        System.out.println("Elapsed (ms): " + elapsed / 1000000);
        System.out.println("Connection worked for " + testUrl);
    } catch (SocketTimeoutException e) {
        System.out.println("More than " + TIMEOUT_VALUE + " elapsed." + testUrl);
    } catch(IOException ioe){
        System.out.println("No timeout for " + testUrl);
    }
}

}

我也尝试过使用 apaches HttpClient 但同样的事情。

private static void testTimeOut(URL testUrl) {
    long start = 0;
    try {
        start = System.nanoTime();

        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_VALUE);
        HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_VALUE);
        HttpClient httpClient = new DefaultHttpClient(httpParams);

        HttpGet httpget = new HttpGet(testUrl.toString());
        HttpResponse response = httpClient.execute(httpget);


        // Get hold of the response entity
        HttpEntity entity = response.getEntity();

        // If the response does not enclose an entity, there is no need
        // to bother about connection release
        if (entity != null) {
            InputStream instream = entity.getContent();
                BufferedReader in = new BufferedReader(new InputStreamReader(instream));
                // do something useful with the response
                 long elapsed = System.nanoTime() - start;
                System.out.println("Elapsed (ms): " + elapsed / 1000000);
                System.out.println("Connection worked for " + testUrl);

            }
    }catch(Exception e){
        long elapsed = System.nanoTime() - start;
        System.out.println("Elapsed (ms): " + elapsed / 1000000);
        System.out.println("No timeout for " + testUrl + " after " + elapsed / 100000);
    }
}

最佳答案

您正在尝试通过 testUrl.openConnection() 打开连接,其中 testUrl 的类型为 URL。好吧,如果你看一下具体的javadoc ,您会注意到 URL.openConnection() 永远不会抛出 SocketTimeoutException。它只能抛出IOException

关于java - 如果服务器没有响应,超时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8178607/

相关文章:

Java Keystore - 从 PKS 文件导入现有通配符证书

java - 关于新订阅者加入 jms 主题的通知

http - 即使生成 '401 Not Authorized' 响应,服务器也必须读取 PUT 请求的正文吗?

c - C 中的 HTTP 身份验证和文件发送

sql - 为什么 Timeout.timeout(sec) 不适用于 activerecord

java - 如何使用 getResourceAsStream 读取 J2ME 中的文件

java - 如果 REST Web 服务调用失败,是否应该使用消息或事件队列稍后重试?

javascript - NODE.JS 和 EXPRESS 中的 URL 重定向处理程序

java - 客户端套接字上的 setSoTimeout 不会影响套接字

Elasticsearch 超时 true 但仍然得到结果