尝试从java代码执行curl命令时出错

时间:2017-10-11 18:00:23

标签: java curl

我在从java代码执行curl命令时遇到了麻烦。我正在使用Runtime.getRuntime()。exec(cmd)构造来执行curl。一切都运行良好,直到我一直坚持接收大约88kb的响应。执行命令是冻结的。我执行命令的代码:

public String executeSimpleBash(String[] cmd) {
        StringBuilder output = new StringBuilder();
        Process p;
        String result;

        try {
            p = Runtime.getRuntime().exec(cmd);
            p.waitFor(40, TimeUnit.SECONDS);

            if (p.exitValue() != 0) {
                result = null;
            } else {
                BufferedReader reader =
                        new BufferedReader(new InputStreamReader(p.getInputStream()));

                String line;

                while ((line = reader.readLine()) != null) {
                    output.append(line).append("\n");
                }

                result = output.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
            result = null;
        }

        return result;
    }

我的命令:

String[] cmd = new String[]{
                "curl",
                "-H",
                "text/xml",
                "--insecure",
                "--max-time",
                "30",
                "-s",
                "-d",
                "languageSelect=enEn&xml=" + xmlBody,
                endpointUrl
        };

0 个答案:

没有答案