如何调用 HttpUrlConnection disconnect()

How can I call HttpUrlConnection disconnect()

我的代码发送 HttpConnection,然后尝试使用 connection.getInputStream()connection.getErrorStream() 来反序列化响应。

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); // getErrorStream()
String content = in.lines().collect(Collectors.joining());

如何将连接传递给 parse(HttpURLConnection connection) 函数并在完成后调用 connection.disconnect()()?

我在想

connection.disconnect();
return MyObject.fromString(connection.getInputStream(), connection.getErrorStream());

但不确定在阅读回复之前断开连接是否有意义。

你可以这样做:

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String content = parse(connection);
connection.disconnect();
// do something with content