【发布时间】:2011-05-29 03:22:20
【问题描述】:
我正在使用 HttpURLConnection 下载 Java 页面。我忘记释放连接,我认为这给我的系统(网络爬虫)造成了一些问题。
现在我做了一些测试,发现在断开连接后,一些连接在 Windows 上的netstat 命令的结果中仍然像 TIME_WAIT。
如何立即释放此连接?
示例代码:
private HttpURLConnection connection;
boolean openConnection(String url) {
try {
URL urlDownload = new URL(url);
connection = (HttpURLConnection) urlDownload.openConnection();
connection.setInstanceFollowRedirects(true);
connection.connect();
connection.disconnect();
return true;
} catch (Exception e) {
System.out.println(e);
return false;
}
}
【问题讨论】: