我怎样才能调用 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

最新更新