我在当前虚拟机上执行的代码同时请求同一个post接口71次,这个post请求需要等待3分钟才有响应信息,当第71次请求时接口内容返回为空的当时需要去远程虚拟机上对用目录下读取实时日志,日志里发现连接失

您可以使用 Java 或者 Shell 脚本来实现您的需求。以下是两种方法的示例:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static void main(String[] args) throws IOException {
        String apiUrl = "http://example.com/api";
        int totalRequests = 71;

        for (int i = 1; i <= totalRequests; i++) {
            // 发送 POST 请求
            HttpURLConnection connection = (HttpURLConnection) new URL(apiUrl).openConnection();
            connection.setRequestMethod("POST");
            // 设置其他请求头或请求体

            // 等待 3 分钟
            try {
                Thread.sleep(3 * 60 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // 读取响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            // 检查响应是否为空
            if (response.toString().isEmpty()) {
                // 读取远程虚拟机上的日志
                // 这里可以使用 SSH 或者其他远程执行命令的方式
                // 在这个示例中,使用了本地命令模拟
                Process process = Runtime.getRuntime().exec("ssh user@remote-vm 'cat /path/to/logfile'");
                BufferedReader logReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String logLine;
                StringBuilder logContent = new StringBuilder();
                while ((logLine = logReader.readLine()) != null) {
                    logContent.append(logLine).append("\n");
                }
                logReader.close();

                // 返回连接失败信息
                System.out.println("Connection failed. Log content: " + logContent.toString());
            } else {
                System.out.println("Response received: " + response.toString());
            }
        }
    }
}
#!/bin/bash

apiUrl="http://example.com/api"
totalRequests=71

for ((i=1; i<=totalRequests; i++)); do
    # 发送 POST 请求
    # 可以使用 curl 或者其他命令发送请求
    # 这里仅示例了 curl 的用法
    response=$(curl -X POST -s $apiUrl)

    # 等待 3 分钟
    sleep 180

    # 检查响应是否为空
    if [ -z "$response" ]; then
        # 读取远程虚拟机上的日志
        # 这里可以使用 SSH 或者其他远程执行命令的方式
        # 在这个示例中,使用了本地命令模拟
        logContent=$(ssh user@remote-vm 'cat /path/to/logfile')

        # 返回连接失败信息
        echo "Connection failed. Log content: $logContent"
    else
        echo "Response received: $response"
    fi
done

请确保替换示例中的 apiUrl、远程虚拟机的用户名、主机名、日志文件路径等信息为实际的值。并根据您的需求调整代码逻辑和错误处理部分。

我在当前虚拟机上执行的代码同时请求同一个post接口71次,请求信息有url和请求体body报文信息,这个post请求需要3分钟才有响应信息,当第71次请求时接口内容返回为空的但是需要去远程虚拟机上对用目录下读取实时日志,日志里发现连接失败的时候把连接失败的信息返回用于这个接口断言用java或者shell怎么写,shell需要接受请求地址和请求体信息把读取到的想要的日志信息返回

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static void main(String[] args) throws IOException {
        String apiUrl = args[0]; // 从命令行参数获取 API 地址
        String requestBody = args[1]; // 从命令行参数获取请求体

        // 发送 POST 请求
        HttpURLConnection connection = (HttpURLConnection) new URL(apiUrl).openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        connection.getOutputStream().write(requestBody.getBytes());

        // 等待 3 分钟
        try {
            Thread.sleep(3 * 60 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 读取响应
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder response = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();

        // 检查响应是否为空
        if (response.toString().isEmpty()) {
            // 读取远程虚拟机上的日志
            // 这里可以使用 SSH 或者其他远程执行命令的方式
            // 在这个示例中,使用了本地命令模拟
            try {
                Process process = Runtime.getRuntime().exec("ssh user@remote-vm 'cat /path/to/logfile'");
                BufferedReader logReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String logLine;
                StringBuilder logContent = new StringBuilder();
                while ((logLine = logReader.readLine()) != null) {
                    logContent.append(logLine).append("\n");
                }
                logReader.close();

                // 返回连接失败信息
                System.out.println("Connection failed. Log content: " + logContent.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("Response received: " + response.toString());
        }
    }
}

请注意,上面的 Java 代码中,通过命令行参数传递了 API 地址和请求体信息。在命令行中调用时,需要提供这两个参数。

#!/bin/bash

apiUrl=$1
requestBody=$2

# 发送 POST 请求
response=$(curl -X POST -H "Content-Type: application/json" -d "$requestBody" -s $apiUrl)

# 等待 3 分钟
sleep 180

# 检查响应是否为空
if [ -z "$response" ]; then
    # 读取远程虚拟机上的日志
    # 这里可以使用 SSH 或者其他远程执行命令的方式
    # 在这个示例中,使用了本地命令模拟
    logContent=$(ssh user@remote-vm 'cat /path/to/logfile')

    # 返回连接失败信息
    echo "Connection failed. Log content: $logContent"
else
    echo "Response received: $response"
fi

在命令行中调用上述 Shell 脚本时,需要提供 API 地址和请求体信息作为参数。例如:

bash./script.sh http://example.com/api '{"key": "value"}' 

请确保替换示例中的 API 地址、远程虚拟机的用户名、主机名、日志文件路径等信息为实际的值,并根据您的需求调整代码逻辑和错误处理部分。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值