我是 Java 和 HttpURLConnection 的新手。我应该在断开连接之前关闭打开的 IO 流吗?如果我关闭流,我是否也需要断开连接?哪种是正确的实施方式?
try {
String uri = "http://localhost:8081/RESTEASY/saju/post/text";
URL url = new URL(uri);
connection =
(HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "text/plain");
connection.setRequestProperty("Content-Type", "text/plain");
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream();
//bla bla
System.out.println(connection.getResponseCode());
InputStream iStream = connection.getInputStream();
//bla bla
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
// os.close(); - IS THIS REQUIRED
// iStream.close(); - IS THIS REQUIRED
connection.disconnect();
}
}