Java - 使用运行时 运行 curl 命令被代理阻止
Java - Using Runtime to run curl command gets blocked by proxy
我正在尝试 运行 使用网络服务的 curl 命令我正在 运行ning:
String curl = "curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";
System.out.println(curl);
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(curl);
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
如果我从 Sysout 复制日志消息并将其粘贴到我的终端上,它 运行 是预期的,但是当我 运行 java 代码时,它似乎return 来自代理的 html 页面未找到服务。
我是否需要为它从 java 添加其他内容到 运行?
问题是您没有让它读取输出。我修改了您的代码,因此它 应该 可以工作。我不是 100% 确定,因为出于某种原因我无法正确测试它。
编辑 - 有效,我只是执行了错误的命令!这应该适合你。
String curl = "curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";
System.out.println(curl);
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(curl);
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); //BufferedReader to read the output
StringBuilder sb = new StringBuilder(); //What will hold the entire console output
String line = ""; //What will hold the text for a line of the output
while ((line = reader.readLine()) != null) { //While there is still text to be read, read it
sb.append(line + "\n"); //Append the line to the StringBuilder
}
System.out.println(sb); //Print out the full output
} catch (Exception e) {
e.printStackTrace();
}
编辑 - 使用 ProcessBuilder
而不是 Runtime
的新代码
String curl = "curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";
ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", curl);
builder.redirectErrorStream(true);
Process p = builder.start();
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
int linenum = 0;
while (true) {
linenum++;
line = r.readLine();
if (line == null) {
break;
}
sb.append(line);
}
System.out.println(sb);
我正在尝试 运行 使用网络服务的 curl 命令我正在 运行ning:
String curl = "curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";
System.out.println(curl);
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(curl);
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
如果我从 Sysout 复制日志消息并将其粘贴到我的终端上,它 运行 是预期的,但是当我 运行 java 代码时,它似乎return 来自代理的 html 页面未找到服务。
我是否需要为它从 java 添加其他内容到 运行?
问题是您没有让它读取输出。我修改了您的代码,因此它 应该 可以工作。我不是 100% 确定,因为出于某种原因我无法正确测试它。
编辑 - 有效,我只是执行了错误的命令!这应该适合你。
String curl = "curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";
System.out.println(curl);
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(curl);
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); //BufferedReader to read the output
StringBuilder sb = new StringBuilder(); //What will hold the entire console output
String line = ""; //What will hold the text for a line of the output
while ((line = reader.readLine()) != null) { //While there is still text to be read, read it
sb.append(line + "\n"); //Append the line to the StringBuilder
}
System.out.println(sb); //Print out the full output
} catch (Exception e) {
e.printStackTrace();
}
编辑 - 使用 ProcessBuilder
而不是 Runtime
String curl = "curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";
ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", curl);
builder.redirectErrorStream(true);
Process p = builder.start();
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
int linenum = 0;
while (true) {
linenum++;
line = r.readLine();
if (line == null) {
break;
}
sb.append(line);
}
System.out.println(sb);