简体   繁体   English

通过 Java 的 curl 命令适用于 Windows 而不是 linux

[英]Curl command through Java works in windows and not in linux

I am trying to execute a curl command using Java using the code below我正在尝试使用以下代码使用 Java 执行 curl 命令

String myUrl= "https://someIp:somePort";
String username = "someusername";
String password = "somepassword";

String command = "curl -k -d \"client_id=someId\" -d \"username="+username+"\" -d \"password="+password+"\"   -d \"grant_type=password\"   -d \"client_secret=\" \""+myUrl+"/myauth/openid-connect/token\"";

Process process = Runtime.getRuntime().exec(command);

ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = process.getInputStream().read(buffer)) != -1) {
        result.write(buffer, 0, length);
}
String response = result.toString(StandardCharsets.UTF_8.name());

This works on windows machine but not on linux.这适用于 Windows 机器,但不适用于 linux。 Is there any difference between linux and windows in the way curl command is executed using exec method ? linux 和 windows 在使用 exec 方法执行 curl 命令的方式上有什么区别吗? Both execution are done using same JRE.两个执行都是使用相同的 JRE 完成的。 On windows I get the token successfully but in Linux I get the following response : Response = {"error":"invalid_request","error_description":"Missing form parameter: grant_type"}在 Windows 上我成功获得令牌,但在 Linux 中我得到以下响应:Response = {"error":"invalid_request","error_description":"Missing form parameter: grant_type"}

Thank you谢谢

After investigation it seems that when someone executes this java code in linux enviroment the curl command is not properly constructed.经过调查,似乎当有人在 linux 环境中执行此 java 代码时, curl 命令没有正确构造。 I used the following code and everything worked fine :我使用了以下代码,一切正常:

String cUrlToKeyCloak = "curl -k -d \"client_id=someId\" -d \"username="+username+"\" -d \"password="+password+"\" -d \"grant_type=password\" "+keyCloakUrl+"/auth/realms/master/protocol/openid-connect/token";

    ProcessBuilder processBuilder = new ProcessBuilder();
    if(!System.getProperty("os.name").contains("Windows"))
        processBuilder.command("bash", "-c", cUrlToKeyCloak );
    else
        processBuilder.command("cmd.exe", "/c", cUrlToKeyCloak );

    String cKeyResponse = "";

    try {

        Process process = processBuilder.start();
        StringBuilder output = new StringBuilder();

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));

        String line;
        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }

        int exitVal = process.waitFor();
        if (exitVal == 0) {
            LOGGER.info("Curl command to keyCloak requested ...");
            LOGGER.info("cKey response = "+output);
            cKeyResponse = output.toString();
        } else {
            LOGGER.error("Curl command to keyCloak executed with error ...");
            LOGGER.info("cKey response = "+output);
            return false;
        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM