gpt4 book ai didi

java - 如何调用 HttpUrlConnection disconnect()

转载 作者:行者123 更新时间:2023-11-30 01:46:51 29 4
gpt4 key购买 nike

我的代码发送 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

关于java - 如何调用 HttpUrlConnection disconnect(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57632927/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com