有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

api无法在java中运行cURL命令

我在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命令吗

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    你试过“放心框架”吗?我以前也有同样的要求,可以用Rest Assured来实现

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