HttpURLConnection请求关闭的问题

quyf 发布于 2014/01/24 14:47
阅读 6K+
收藏 0

HttpURLConnection用于网络请求,基本就是像下面这么写了,os,br都close()了,HttpURLConnection 资源是否需要关闭呢,没有close()方法直接给调的,有个disconnect()

URL ur = new URL( url );

HttpURLConnection conn = (HttpURLConnection) ur.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setConnectTimeout(30000);
OutputStream os = conn.getOutputStream();
os.write( params.getBytes());
os.flush();

os.close();

BufferedReader br  = new BufferedReader(new InputStreamReader( conn.getInputStream()));
StringBuilder sb =new StringBuilder();
String lines;

while( (lines = br.readLine())!=null ){
sb.append( lines );
}
br.close();
return sb.toString();

加载中
0
wangshihao
wangshihao
遇到了同样的问题,不知道楼主有没有解决
quyf
quyf
url.disconnect();
返回顶部
顶部