简体   繁体   English

使用 java 发送 curl 命令 8

[英]sending curl command using java 8

Below is my code in which I am trying to send a curl command to the indicated url but using java but when I run the code it won't execute and sends a curl command please help to fix it or tell me where i did wrong: Below is my code in which I am trying to send a curl command to the indicated url but using java but when I run the code it won't execute and sends a curl command please help to fix it or tell me where i did wrong:

String command="curl 'http://ipaddress:port/smshttpquery/qs?REQUESTTYPE=SMSSubmitReq&USERNAME=myapp&PASSWORD=app&PHON=XXXXXXXXX&MESSAGE=TEST&ADDRESS=MYAPP&TYPE=4'";
    ProcessBuilder processBuilder=new ProcessBuilder(command.split(" "));
    try {
        processBuilder.start();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

The linux command I want to run is我要运行的 linux 命令是

curl 'http://ipaddress:port/smshttpquery/qs?REQUESTTYPE=SMSSubmitReq&USERNAME=myapp&PASSWORD=app&PHON=XXXXXXXXX&MESSAGE=TEST&ADDRESS=MYAPP&TYPE=4'

Assuming you showed us all of your code, you won't see any result simply because your code nowhere reads the output stream of the process (process.getInputStream()).假设您向我们展示了所有代码,您将看不到任何结果,因为您的代码无处读取进程的 output stream (process.getInputStream())。

There are other pitfalls on the way to success, though.不过,通往成功的道路上还有其他陷阱。

public static String HttpCall(String link) throws IOException{
    String response = "";
    String beginPoint = link;
    URL url = new URL(beginPoint);

    try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
        String line = "";
        while ((line = reader.readLine()) != null) {
            response += line;
        }
        reader.close();
        response = response.replace("\n", "").replace("\r", "");
    }
    return response;

} }

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

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