gpt4 book ai didi

Android:HttpURLConnection 不会断开连接

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:24 26 4
gpt4 key购买 nike

通过在服务器上运行netstat:
直到几天前:我可以看到连接ESTABLISHED只有大约一秒钟,然后它就会从列表中消失
NOW:它保持 ESTABLISHED 大约 10 秒,然后进入 FIN_WAIT1FIN_WAIT2

Android代码一样,服务器还是一样

是否有可能某种 Android 更新可能改变了一些事情?

我真的无法解释。

我报告下面的代码。 urlConnection.disconnect() 被执行,但连接仍然在服务器上建立。

    HttpURLConnection urlConnection = null;
System.setProperty("http.keepAlive", "false");

try {
URL url = new URL(stringUrl);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream instream = new BufferedInputStream(urlConnection.getInputStream());
...
instream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection!=null) {
urlConnection.disconnect();
}
}

最佳答案

当输入流上的所有数据都被消耗时,连接会自动释放并添加到连接池中。底层套接字连接没有释放,假设连接将在不久的将来被重用。在 finally block 中调用 disconnect 是一种很好的做法,因为它负责在出现异常 时释放连接。

这里是FixedLengthInputStream的read方法的实现:

@Override public int read(byte[] buffer, int offset, int count) throws IOException {
Arrays.checkOffsetAndCount(buffer.length, offset, count);
checkNotClosed();
if (bytesRemaining == 0) {
return -1;
}
int read = in.read(buffer, offset, Math.min(count, bytesRemaining));
if (read == -1) {
unexpectedEndOfInput(); // the server didn't supply the promised content length
throw new IOException("unexpected end of stream");
}
bytesRemaining -= read;
cacheWrite(buffer, offset, read);
if (bytesRemaining == 0) {
endOfInput(true);
}
return read;
}

当bytesRemaining变量变为0时,endOfInput被调用,它将进一步调用带有 true 参数的 release 方法,这将确保连接被合并。

protected final void endOfInput(boolean reuseSocket) throws IOException {
if (cacheRequest != null) {
cacheBody.close();
}
httpEngine.release(reuseSocket);
}

这是 release方法实现。 最后一个 if 检查确保连接是否需要关闭或添加到连接池以供重用。

public final void release(boolean reusable) {
// If the response body comes from the cache, close it.
if (responseBodyIn == cachedResponseBody) {
IoUtils.closeQuietly(responseBodyIn);
}
if (!connectionReleased && connection != null) {
connectionReleased = true;
// We cannot reuse sockets that have incomplete output.
if (requestBodyOut != null && !requestBodyOut.closed) {
reusable = false;
}
// If the headers specify that the connection shouldn't be reused, don't reuse it.
if (hasConnectionCloseHeader()) {
reusable = false;
}
if (responseBodyIn instanceof UnknownLengthHttpInputStream) {
reusable = false;
}
if (reusable && responseBodyIn != null) {
// We must discard the response body before the connection can be reused.
try {
Streams.skipAll(responseBodyIn);
} catch (IOException e) {
reusable = false;
}
}
if (!reusable) {
connection.closeSocketAndStreams();
connection = null;
} else if (automaticallyReleaseConnectionToPool) {
HttpConnectionPool.INSTANCE.recycle(connection);
connection = null;
}
}
}

注意:我之前已经回答了几个与 HttpURLConnection 相关的 SO 问题,这可以帮助您理解底层实现。以下是链接:Link1Link2 .

关于Android:HttpURLConnection 不会断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24598769/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com