【发布时间】:2018-10-24 07:05:28
【问题描述】:
我有一个方法 (getRequest) 可以打开 URLConnection 并返回 InputStream,这里我没有关闭 URLConnection。
在 sendHTTPMessage 中,我将关闭 InputStream 和 ObjectInputStream。
那么它会产生问题,因为我没有关闭 URLConnection,据我了解它打开了与服务器的套接字连接吗?
public InputStream getRequest(String url) throws IOException {
URL url = new URL(url);
URLConnection con = url.openConnection();
con.setUseCaches(false);
this.sendHeaders(con);
return con.getInputStream();
}
private Object sendHTTPMessage(HashMap<String, Object> params) {
Object resultobj = null;
InputStream in = null;
ObjectInputStream ois = null;
try {
in = sendGetMessage(params);
if (in != null) {
ois = new ObjectInputStream(in);
serviceResult = (Object)ois.readObject();
}
} catch (Exception var14) {
logger.error("Error during closing :", var14);
} finally {
try {
if (in != null) {
in.close();
}
if (ois != null) {
ois.close();
}
} catch (IOException var13) {
logger.error("Error during closing :", var13);
}
}
return resultobj;
}
【问题讨论】:
标签: java spring http connection urlconnection