【问题标题】:establish http request with Timed out - Java建立超时的http请求 - Java
【发布时间】:2020-10-24 18:59:52
【问题描述】:

我正在寻找一种通过 java 建立 HTTP 请求以确保服务器处于活动状态的方法。

例如我想扫描IP地址范围192.168.1.1-255并打印与活动服务器的日志。,

当 HTTP 响应由于某种原因延迟时,我想setTimeOut 3 秒。

我尝试过这样做:

try {
            Socket s = new Socket(InetAddress.getByName("192.168.1.2"), 80);
            s.setSoTimeout(3 * 1000);
            PrintWriter pw = new PrintWriter(s.getOutputStream());
            pw.println("GET / HTTP/1.1");
            pw.println("Host: stackoverflow.com");
            pw.println("");
            pw.flush();
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String t;
            while ((t = br.readLine()) != null) System.out.println(t);
            br.close();
        } catch (SocketTimeoutException e) {
            System.out.println("Server is dead.");
        } catch (ConnectException e) {
            System.out.println("Server is dead.");
        }

但是当请求花费的时间超过 3000 毫秒时,它似乎根本没有等待。

谢谢!

【问题讨论】:

标签: java sockets httprequest connection-timeout


【解决方案1】:

我认为您混淆了不同的超时。如果您想在三秒后没有任何响应就中止连接尝试,那么您应该按如下方式建立连接:

Socket clientSocket = new Socket();
clientSocket.connect(new InetSocketAddress(target, 80), 3 * 1000);

其中target 是任何IP 地址。以下行实质上设置了连接建立后读取/等待输入流的超时值。所以它对建立连接本身没有影响。但是,在建立连接后,它会在三秒后中断“读取输入流”步骤(通过抛出异常)。

clientSocket.setSoTimeout(3 * 1000);

但是,如果您还想限制读取输入流的时间而不抛出异常,那么您需要一个成本解决方案: Is it possible to read from a InputStream with a timeout?

以下运行示例在我的本地网络中运行良好。它尝试连接最多三秒钟,并检测到所有正在运行的网络服务器。

public class Main {
    public static void main(String[] args) {
        String net = "192.168.168."; // this is my local network
        for (int i = 1; i < 255; i++) { // we scan the range 1-255
            String target = net + i;
            System.out.println("Try to connect to: " + target);
            try {
                Socket clientSocket = new Socket();

                // we try to establish a connection, timeout is three seconds
                clientSocket.connect(new InetSocketAddress(target, 80), 3 * 1000);
   
                // talk to the server
                PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
                out.println("GET / HTTP/1.1");
                out.println("Host: stackoverflow.com");
                out.println("");
                out.flush();
                BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                String t;
                while ((t = br.readLine()) != null) System.out.println(t); // We print the answer of the server
                br.close();
                clientSocket.close();

                // server seems to be alive
                System.out.println("> Server is alive");
            } catch (SocketTimeoutException | ConnectException e) {
                System.out.println("> Server is dead");
            } catch (Exception e) { // This is not nice but this is also just a demo
                e.printStackTrace();
            }
        }
    }
}

输出(摘录):

Try to connect to: 192.168.168.1
> Server is dead
Try to connect to: 192.168.168.2
> Server is dead
...
Try to connect to: 192.168.168.23
(answer of the server)
> Server is alive
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-05
    • 2010-12-12
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    相关资源
    最近更新 更多