Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HTTP请求读取响应体_Java_Http_Httprequest - Fatal编程技术网

Java HTTP请求读取响应体

Java HTTP请求读取响应体,java,http,httprequest,Java,Http,Httprequest,如何读取HTTP请求的正文?。我在寻找一个简单的方法,我认为这是最简单的 URL url = new URL("http://www.y.com/url"); InputStream is = url.openStream(); try { /* Now read the retrieved document from the stream. */ ... } finally { is.close(); } 但是如何在控制台中读取或打印错误?有没有更简单的方法来发出请求并阅读响应正

如何读取HTTP请求的正文?。我在寻找一个简单的方法,我认为这是最简单的

URL url = new URL("http://www.y.com/url");
InputStream is = url.openStream();
try {
  /* Now read the retrieved document from the stream. */
  ...
} finally {
  is.close();
}
但是如何在控制台中读取或打印错误?有没有更简单的方法来发出请求并阅读响应正文

我怎样才能在控制台上阅读或打印

使用并尝试以下资源:

URL url = new URL("http://www.y.com/url");
try(BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) {
    br.lines().forEach(System.out::println);
}

如果使用Java 9+,则可以使用HttpClient、HttpRequest和HttpResponse

HttpClient=HttpClient.newHttpClient; HttpRequest request=HttpRequest.newBuilder.urinew URIyour url.GET.build; HttpResponse response=client.sendrequest,BodyHandlers.OfsString; System.out.printlnresponse.body;