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

发布于 2024-12-16 13:25:28 字数 3100 浏览 0 评论 0原文

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

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

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

public class 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);
    }
}

I have the problem that I never get a SocketTimeOutException when server not responding. After 30 seconds I get an IOException instead.

What do I need to do to get a timeout when server is not responding?

This is my code where the first URL works and the second results in an IOException instead of SocketTimeOutException.

public class 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);
    }
}

}

I have also tried with apaches HttpClient but same thing there.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔戏命师 2024-12-23 13:25:29

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

You're trying to open a connection via testUrl.openConnection() where testUrl is of type URL. Well, if you have a look at the specific javadoc, you'll notice that URL.openConnection() will never throw a SocketTimeoutException. It can only throw an IOException.

对岸观火 2024-12-23 13:25:29

您可以捕获 IOException 并在 catch 块中抛出一个新的 SocketTimeOutException

You can catch the IOException and throw a new SocketTimeOutException in the catch block

欲拥i 2024-12-23 13:25:29
  1. 连接对象是通过调用 URL 上的 openConnection 方法创建的。
  2. 操纵设置参数和一般请求属性。
  3. 使用 connect 方法建立与远程对象的实际连接。
  4. 远程对象变得可用。可以访问远程对象的标头字段和内容。
  1. The connection object is created by invoking the openConnection method on a URL.
  2. The setup parameters and general request properties are manipulated.
  3. The actual connection to the remote object is made, using the connect method.
  4. The remote object becomes available. The header fields and the contents of the remote object can be accessed.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文