建立超时的 http 请求 - Java

establish http request with Timed out - Java

我正在寻找一种通过 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: whosebug.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 毫秒时,它似乎根本没有等待。

谢谢!

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

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

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

clientSocket.setSoTimeout(3 * 1000);

但是,如果你还想限制读取输入流的时间而不抛出异常,那么你需要一个 costum 解决方案: 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: whosebug.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
...