【发布时间】:2025-12-06 13:00:01
【问题描述】:
我有一个 URLConnection,我想根据响应代码取消它而不读取任何数据。我密切关注android training 构建了以下最小示例 由于没有连接被释放回句柄池以供重用,因此向服务器发出大量请求
private String downloadUrl(String myurl) throws IOException {
InputStream is = null;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
Log.d(TAG, "The response code is: " + response);
is = conn.getInputStream();
// Do not read anything //String contentAsString = readIt(is, len);
String contentAsString = "notReadingAnything";
return contentAsString;
} finally {
if (is != null) {
is.close();
}
}
}
private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
try {
String result = new String();
for (int i=0; i<100; i++) {
result += downloadUrl(urls[0]);
}
return result;
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
@Override
protected void onPostExecute(String result) {
Log.d(TAG, "The response is: " + result);
}
}
尽管docs 明确声明
但是如果响应体很长,看到开头就对其余部分不感兴趣,可以关闭 InputStream
如果我不读取流,服务器会迅速达到其最大连接数 (50) 并达到 99% 的工作负载,但如果我读取它,则工作正常。我的错误是什么?
编辑:迄今为止的解决方案尝试失败(感谢@Blackbelt 为他们中的大多数)
- 在
finally块中调用conn.disconnect() - 在
finally块中调用conn.disconnect()而不是is.close() - 在第一次调用之前设置
System.setProperty("http.keepAlive", "false"); - 连接前设置
conn.setRequestProperty("Connection", "Close"); - 在使用的后端服务器 (Civetweb) 上设置
"{enable_keep_alive", "no"}
【问题讨论】:
-
仅供参考:查看
conn.getResponseCode()的实现,我还注意到一个非常有趣的陷阱:它检索 inputStream 本身,但当然不会关闭它。所以如果你使用这种方法,你必须事后检索流并自己关闭它。
标签: java android httpconnection