【问题标题】:Cannot run the cURL command in java无法在 java 中运行 cURL 命令
【发布时间】:2018-07-06 03:48:38
【问题描述】:

我在我的 java 类中运行下面的代码,理想情况下它应该返回一个不记名令牌....但是当我运行代码时,我得到了异常

下面是我正在运行的代码

String[] command = {"curl","-v","-
X","POST","https://<Domain name>/oauth2/token","-H","cache-control: 
no-cache","-H","content-type: application/x-www-form-urlencoded","-
H","postman-token: a1f5e569-cbc7-63cc-8667-45d85d74784b","-d","client_id=XXXXXXX","&","client_secret=XXXXXXXXXX","&","grant_type=client_credentials"};
for (String s: command)
            System.out.print(s); 
        ProcessBuilder process = new ProcessBuilder(command);
         Process p;
try {

                p = process.start();
     }

我得到的例外是

java.io.IOException: Cannot run program "curl": CreateProcess error=2, The system cannot find the file specified

有人可以告诉我是什么问题或如何从 JAVA 运行 curl 命令

提前致谢

【问题讨论】:

  • curlPATH 上吗?
  • yes 这是实际的 cURL 命令 ` curl -v -X POST https:///oauth2/token -H 'cache-control: no-cache' -H 'content-type : application/x-www-form-urlencoded' -H 'postman-token: a1f5e569-cbc7-63cc-8667-45d85d74784b' -d 'client_id=XXXX&client_secret=XXXXXX&grant_type=client_crede‌​ntials' `
  • 您使用的是 Linux、Windows 还是 Mac?
  • 我正在使用 Windows,当我在 Windows 中运行它时会出错。但我正在使用eclipse运行。此外,当我在 linux 机器上运行它时,我的应用程序正在运行,但当控制达到 p = process.start(); 时没有结果;我的意思是它不会进入下一行@gil.fernandes
  • 你能不能从 Eclipse 运行这个 oneliner,看看 curl 是否确实在路径中:new ProcessBuilder("cmd", "/k", "path").redirectOutput(ProcessBuilder.Redirect.INHERIT).start();。这会打印出您环境中的路径。

标签: java api curl get httpclient


【解决方案1】:

您是否尝试过 Rest Assured Framework?我曾经有过同样的要求,并且可以使用 Rest Assured 来实现。

RestAssured.baseURI = CURLURL;
Response r = (Response) given().relaxedHTTPSValidation().contentType("application/json").body(curlRequest)
.when().post("");
return r.getBody().asString();

【讨论】:

  • 很抱歉,我无法理解,您能帮我提供更详细的代码吗
猜你喜欢
  • 2021-10-26
  • 2016-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 2016-11-19
相关资源
最近更新 更多