gpt4 book ai didi

java - Httpsurlconnection 不关闭套接字

转载 作者:行者123 更新时间:2023-12-01 04:54:13 27 4
gpt4 key购买 nike

我尝试过解决这个问题,但没有找到好的解决方案。我刚刚用 httsurlconnection 打开了一个套接字并关闭了。但是,我打开的套接字没有关闭,并且套接字状态为 CLOSE_WAIT。这是我的代码和空气日志。我的代码有什么问题吗?

    String https_url = "https://www.google.com";

URL url = new URL(https_url);

try {
con = (HttpsURLConnection) url.openConnection();
con.setRequestProperty("Connection", "close");
if(con!=null){
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String input = br.readLine();
while(input.length() != 0)
{

input = br.readLine();
}
br.close();
con.disconnect();

}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

您肯定会从此代码中得到 NullPointerException。在执行其他操作之前,您必须测试 readLine() 的结果是否为 null。我不明白测试 HTML 页面中的行有什么意义。

关于java - Httpsurlconnection 不关闭套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14431458/

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