【问题标题】:unable to execute curl command in java无法在java中执行curl命令
【发布时间】:2016-06-22 01:13:37
【问题描述】:

这是我试图在 Eclipse 中运行的 curl 命令的代码 sn-p:

ProcessBuilder pb = new ProcessBuilder(
                    "curl",
                    "-XPOST",
                    "curl https://login.xyz.com/v1/oauth/token -H \"Accept: application/json\" --data 'client_id=<clientId>' --data 'client_secret=<clientSecret>' --data 'redirect_uri=localhost' --data 'code=<code>' ");

pb.redirectErrorStream(true);
Process p = pb.start();

InputStream is = p.getInputStream();

BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));

String line = null;
while ((line=r.readLine()) != null) {

    System.out.println(line);
}

我应该得到一个 json 输出,如下所示:

{"data":
{
"sfEntityType":"Token",
"accessToken":<token>,
"expiryTime":<time in seconds>
},
"status":{"succeeded":true}
}

但我得到这样的输出:

curl: (1) Protocol "curl https" not supported or disabled in libcurl
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0

  curl: (6) Could not resolve host: application

我该如何解决这个问题?

【问题讨论】:

  • 您的问题应该更加具体。您确实没有从 java 调用 curl 时遇到问题。您的问题是 curl 输出看起来不像您期望的那样。无论您是从 java、python 还是直接在命令行调用 curl,这都无关紧要!

标签: java curl


【解决方案1】:

为了以某种易于阅读的方式从 curl 获取输出,您应该使用 -w 标志,然后在引号中指定您需要的内容:

-w"耗时: %{time_total} 本地IP: %{local_ip} 本地端口: %{local_port} 大小下载: %{size_download} 远程IP: %{remote_ip} 远程端口: %{remote_port} 速度上传: %{speed_upload} Bps "

特别是在 Linux 上,您需要在预期的最后一个字符之后留一个尾随空格。

【讨论】:

  • 关于 curl 的更多信息请参考man page
【解决方案2】:

curl: (1) libcurl 中不支持或禁用协议“curl https” 因为你使用的 curl 包不支持 https 协议。只需在命令行中键入 curl -V 并查看列出的协议。

解决办法:

下载并安装启用 SSL 的不同 libcurl 包。

【讨论】:

    猜你喜欢
    • 2018-07-06
    • 1970-01-01
    • 2014-11-20
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 2021-10-26
    相关资源
    最近更新 更多