【问题标题】:Executing curl command in java application在java应用程序中执行curl命令
【发布时间】:2016-06-01 15:33:45
【问题描述】:

我正在尝试通过我的 java 应用程序执行 curl 命令(在终端中工作)。我尝试过使用流程生成器和Runtime.exec()。我可以连接,但响应要么说

{"result":0,"reason":"Unknown Json request format"} 

{"result":0,"reason":"Invalid client request Json string"}.

这是在终端中运行的 curl 命令:

curl -X POST -d '{"command":"get","user":"R16","id":5552}' "http://<Ex.webaddress>"

运行时,连接失败

String testCurlCommand = "curl -X POST -d '{\"command\":\"get\",\"user\":\"R16\",\"id\":5552}' \"http://<Ex.webaddress\"";try {
        //final Process terminal = curlCommand.start();
        Process p = Runtime.getRuntime().exec(testCurlCommand);
        try {
            String responseString = readInputStream(p.getInputStream());
            JSONObject job = new JSONObject(responseString);

            // job.getString("Parameter")
            statusLabel.setText("Command Result: " + job.toString());
            connectionStatusLabel.setText("Connection Status: Successful");
        } catch (JSONException e) {
            statusLabel.setText("Command Result: 0");
            connectionStatusLabel.setText("Connection Status: Failed");
        }
    } catch (IOException ex) {
        statusLabel.setText("Command Result: 0");
        connectionStatusLabel.setText("Connection Status: Failed");
    }

通过删除网址周围的引号来更改字符串 testCurlCommand 时,连接正常,但我得到“未知的 Json 请求格式”

String testCurlCommand = "curl -X POST -d '{\"command\":\"get\",\"user\":\"R16\",\"id\":5552}' http://<Ex.webaddress";

我试过删除转义的引号,但还是一样。我也尝试过使用流程构建器:

ProcessBuilder testCurlCommand = new ProcessBuilder("curl", "-X", "POST", "-d", "'{\"command\":\"get\",\"user\":\"R16\",\"id\":\"5552\"}'", "\"http://<examplewebaddress>\"")

我认为这与格式有关(额外引用或其他内容),但我不确定。非常感谢任何帮助。我将在下面添加 readInputStream 函数:

static private String readInputStream(InputStream inputStream) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            inputStream, "UTF-8"));
    String tmp;
    StringBuilder sb = new StringBuilder();
    while ((tmp = reader.readLine()) != null) {
        sb.append(tmp).append("\n");
    }
    if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\n') {
        sb.setLength(sb.length() - 1);
    }
    reader.close();
    return sb.toString();
}

【问题讨论】:

  • 为什么不直接使用 Java 进行 POST 呢?我会更简单,更容易维护和跨平台
  • 我不知道该怎么做,你有什么我应该访问的链接吗?谢谢

标签: java json curl


【解决方案1】:

对我来说,这看起来像是一个引号问题。 尽量保持一致,并尽量用反斜杠转义以下引号

curl -X POST -d '{"command":"get","user":"R16","id":5552}' 'http://<Ex.webaddress>'

PS:我只是把url的引号从"改成了'

【讨论】:

    【解决方案2】:

    我知道的最好方法是使用DavidWebb

    你可以这样做:

    Response<JSONObject> result = Webb.create().post("http://<Ex.webaddress>")
            .body("{\"command\":\"get\",\"user\":\"R16\",\"id\":5552}").asJsonObject();
    JSONObject job = result.getBody();
    

    【讨论】:

    猜你喜欢
    • 2016-06-22
    • 1970-01-01
    • 2011-01-16
    • 2015-06-15
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多