无法在Java中运行cURL命令

dgtucam1  于 2024-01-05  发布在  Java
关注(0)|答案(1)|浏览(215)

我在我的java类中运行下面的代码,理想情况下它应该返回一个承载令牌..
下面是我运行的代码:

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

字符串
我得到的例外是
java.io.IOException:Cannot run program“curl”:无法运行程序“curl”:进程错误=2,系统找不到指定的文件
有人能告诉我问题是什么或者如何从JAVA运行curl命令吗?

niknxzdl

niknxzdl1#

你有没有尝试过Rest Assured Framework?我以前也有同样的要求,可以用Rest Assured来实现。

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

字符串

相关问题