在java 8中执行CURL命令



我有一个问题,你可能会帮我,我正在尝试在Java 8中执行CURL命令,我在运行时的代码在中变空

writer.toString((;

,但如果我在终端中运行该命令,则不会出现代理问题。

但如果我使用带-o的Curl,我会在服务器上创建临时文件,但这不好,因为我有数百个链接需要验证,有人知道我在这里缺少什么?

String url = "https://" + serverName + ":" + port + "/api/doc.json";

serviceRecord.setHost(lstHost);
Process process = Runtime.getRuntime().exec("curl -k " + url);
InputStream inputStream = process.getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, "UTF-8");
String text = writer.toString();

这是一个到java的curl转换
我不是个爪哇人。

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
class Main {
public static void main(String[] args) throws IOException {
URL url = new URL("https://example.com:80/api");
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("POST");
InputStream responseStream = httpConn.getResponseCode() / 100 == 2
? httpConn.getInputStream()
: httpConn.getErrorStream();
Scanner s = new Scanner(responseStream).useDelimiter("\A");
String response = s.hasNext() ? s.next() : "";
System.out.println(response);
}
}

最新更新