[英]wget not displaying output through Runtime.getRuntime.exec(String[] commandArray)
[英]Unable to send JSON string through java Runtime.getRuntime.exec();
我正在嘗試使用CURL命令和Runtime.getRuntime.exec()函數發送JSON字符串。
我的JSON字符串類似於:
String jsonString = "{\"object\":[\"something\",\"another something\"]}"
我正在嘗試使用以下功能發送此字符串:
Process p;
p = Runtime.getRuntime().exec(new String[] {"curl","someURL","-H","Content-Type:application/json","-d",jsonString,"-u","something:something"}
一旦執行以下行並解析了輸出,就會收到一條錯誤消息,指出JSON文檔無效。 當我使用命令行嘗試相同的命令時,它工作正常。 我認為問題在於JSON字符串,因為轉義字符也作為JOSN數據的一部分發送,因此無效的JSON數據輸出也已發送。
是否有我做錯的事情,或者我必須執行命令的其他方式。
只是嘗試像修改並工作
String jsonString = "{\"object\":[\"something\",\"another something\"]}";
ProcessBuilder ps = new ProcessBuilder(new String[] { "curl", "http://localhost:8338", "-H",
"Content-Type:application/json", "-d", jsonString, "-u", "something:something" });
ps.redirectErrorStream(true);
Process pr = ps.start();
BufferedReader in = new BufferedReader(new
InputStreamReader(pr.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
pr.waitFor();
in.close();
System.exit(0);
對於服務器端,我使用了Pippo webframework,它返回了一個OK字符串
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:- -:-- 0
100 46 0 2 100 44 329 7247 --:--:-- --:--:-- --:--:-- 7333
OK
服務器端代碼:
public class PippoApplication extends Application {
private final static Logger log = LoggerFactory.getLogger(PippoApplication.class);
@Override
protected void onInit() {
POST("/", new RouteHandler() {
@Override
public void handle(RouteContext routeContext) {
System.out.println(routeContext.getRequest().getBody());
routeContext.send("OK");
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.