【问题标题】:java - using curl with Runtime.getRuntime().execjava - 将 curl 与 Runtime.getRuntime().exec 一起使用
【发布时间】:2012-09-09 07:52:21
【问题描述】:

我想在 java 程序中使用 Runtime.getRuntime().exec 来使用 curl

完整的 sn-p 低于但我的问题归结为当我在 Runtime.getRuntime().exec(command 中使用 curl command 时收到错误响应)但是当我 System.out.println command 时,将其复制并粘贴到 shell 并在那里执行它工作正常。

System.out.println(command);
Runtime.getRuntime().exec(command);

什么可能导致运行时 exec 和在 shell 中执行命令之间出现这种不同的结果。

感谢任何提示

马丁

更新:

  • 我在使用 Runtime 时得到的错误响应是:{"error":"unauthorized"}

  • 正如我从似乎不清楚的命令中看到的那样。我没有得到 curl 命令运行的任何异常,但 json 响应如上所示。


String APP_ID = "XXX";
String REST_API_KEY = "YYY";

String header = "-H \"X-Parse-Application-Id: " + APP_ID
        + "\" -H \"X-Parse-REST-API-Key: " + REST_API_KEY
        + "\" -H \"Content-Type: application/zip\" ";

public void post2Parse(String fileName) {

    String outputString;

    String command = "curl -X POST " + header + "--data-binary '@"
            + fileName + "' https://api.parse.com/1/files/" + fileName;

    System.out.println(command);

    Process curlProc;
    try {
        curlProc = Runtime.getRuntime().exec(command);

        DataInputStream curlIn = new DataInputStream(
                curlProc.getInputStream());

        while ((outputString = curlIn.readLine()) != null) {
            System.out.println(outputString);
        }

    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

【问题讨论】:

  • 发布你得到的异常/错误
  • {"error":"unauthorized"}
  • 你有例外吗?它有哪个错误代码和消息?还是输出显示未经授权?
  • 已发布响应 json 的错误响应
  • 我的问题更多的是关于 Runtime.getRuntime().exec() 命令执行和在 shell 中执行相同命令之间的区别

标签: java curl command


【解决方案1】:

我没有直接的答案,但有点意思是撇号的编码方式不同。虽然是一个老问题,但我会回答以供将来参考,也许以后有人可以解释完整的答案。

当我以类似的方式测试我们的 API 时,我注意到以下几点:

curl -i http://localhost:8080/auth/login -d 'identifier=test@example.com&password=test'

这个命令,当从 shell 发送时,在服务器中结束为:

identifier=test@example.com,\npassword=test,\n

但是当从 Runtime.getRuntime().exec(command) 发送时,它最终是:

'identifier=test@example.com,\npassword=test',\n

如果我更改 curl 命令并删除撇号,它会起作用(由于此命令的本质)

 curl -i http://localhost:8080/auth/login -d identifier=test@example.com&password=test

因此,一个既成的猜测是,如果问题中的代码更改为这个,如果文件名不包含空格,它实际上可能会工作......:

String command = "curl -X POST " + header + "--data-binary @"
        + fileName + " https://api.parse.com/1/files/" + fileName;

一种选择是使用带有输入数组的执行命令,如下所述:Runtime Exec seems to be ignoring apostrophes 结合解析 curl 命令(除非硬编码),如下所述:Split string on spaces in Java, except if between quotes (i.e. treat \"hello world\" as one token)

【讨论】:

  • 您救了我的命,先生。我在这堵墙上撞了几个小时。我想简单地从 gradle 上传一个带有 curl 的文件(作为一种快速实现)这比预期的要长 干杯!
  • 你救了我的命。非常感谢。
【解决方案2】:

我猜你从 Curl 得到一个错误代码而不是异常,再次检查 Curl API,你猜你会发现任何缺少的参数,如“用户”或其他东西。

您可以尝试检查this答案中提到的一些提示。

【讨论】:

  • 谢谢 - 是的,不是例外,而是如上所述的错误响应。同样的 curl 命令在 shell 中执行时可以工作,因此在 Curl API 的那一侧似乎真的不是问题......
  • 我看到了另一个帖子,但为了简单起见,我决定按照那里的一个 cmets 中的建议去运行时
猜你喜欢
  • 2012-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 1970-01-01
相关资源
最近更新 更多