HttpURLConnection getInputStream
时间: 2023-10-25 17:21:30 浏览: 36
`HttpURLConnection.getInputStream()` is a method in Java that returns an input stream that reads from this open connection. The input stream contains the server's response to the HTTP request made by the client. You can use this method to read the response body returned by the server.
Here's a simple example of using `HttpURLConnection.getInputStream()`:
```java
URL url = new URL("https://example.com");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
InputStream inputStream = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
con.disconnect();
```
This example sends a GET request to `https://example.com`, gets the input stream from the response, and reads the response body line by line using a `BufferedReader`. Finally, it closes the reader and disconnects the connection.