java - 在Java中使用非持久连接时是否应该调用HttpUrlConnection的disconnect()方法?

标签 java httpurlconnection multipart-mixed-replace

我想从服务器获取流数据。服务器以 multipart/x-mixed-replace 格式发送数据,并且它具有 Connection: close 属性。 Connection: close 表示客户端在收到分块数据时必须关闭连接。我说得对吗?

或者,由于数据正在流式传输,连接未关闭(服务器每次都发送分块数据,我不是每次都向服务器发送 get 请求。或者这是在后台完成的?)。因此,在我调用 inputStream.close() 方法之前,连接不会随时关闭。对吗?

此外,如果服务器在任何时候关闭,http url 连接将抛出IOException。这种情况下,我必须调用http url连接的disconnect()方法吗?或者,我应该只调用 inputStream.close() 吗?

如何随时安全地关闭HttpURLConnection

最佳答案

Connection: close means, it wants that client must be close the connection when receive the chunked data. Am I right?

不完全是。 HTTP 消息中的 Connection: close header 是提供信息的,而不是规定性的。它通知接收者服务器打算在发送响应后关闭连接(请参阅 RFC 7230, section 6.1 )。用户没有义务采取特定操作来响应,但在收到 HTTP 有效负载后,通过不再尝试通过该连接进行任何进一步的通信,可以节省一些时间。然而,在实践中,是的,客户端在收到响应后,也应该关闭其一端的应用层连接,因为在关闭之前,它将无缘无故地占用相关的系统资源.

但是,如果您正在使用 HttpURLConnection 和/或从其中获取的 InputStream,那么这些都不是您真正关心的。每its documentation :

Each HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server may be transparently shared by other instances. Calling the close() methods on the InputStream or OutputStream of an HttpURLConnection after a request may free network resources associated with this instance but has no effect on any shared persistent connection. Calling the disconnect() method may close the underlying socket if a persistent connection is otherwise idle at that time.

也就是说,HttpURLConnection 为您管理持久连接的详细信息。

你继续,

Or, connection not closed because of data is streaming(server sends chunked data each time, I'm not send get request to the server at each time. Or is this done in the background?).

看来您只是意味着服务器没有指定内容长度,并在较长的时间内发送不确定长度的响应。在这种情况下,Connection header 可能没有太多实际相关性。

So, connection is not closing at any time until I close the inputStream.close() method. Right?

服务器通常不会在发送完完整响应之前关闭连接。原则上,如果响应具有无限长度,则除了服务器关闭或失败之外,没有理由期望服务器从其一端发起连接关闭。

Also, if server is down at any time, http url connection will be thrown the IOException.

也许吧。如果首先尝试建立连接失败,那么您可能会遇到某种类型的 IOException。如果服务器在传递响应时发生故障,那么您可能会收到异常,但您也可能只会看到流的结尾。

In this case, Must I call the disconnect() method of the http url connection? Or, should I call just inputStream.close()?

您不需要 disconnect(),如果您这样做,那么这只是建议性的,如上面引用的文档中所述。如果您到达流的末尾,那么您确实应该关闭它。如果在读取流时抛出 IOException,那么最好尝试 close() 流,但也要做好失败的准备,因为流可能处于不一致的状态.

How can I close the http url connection safely at any time?

一旦您实际将 HttpURLConnection 实例连接到底层资源,关闭其流就足以表明您已完成操作。连接之前,您无需执行任何操作。

关于java - 在Java中使用非持久连接时是否应该调用HttpUrlConnection的disconnect()方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57449584/

相关文章:

java - 为什么 ARToolKit Android 在 Moto X 2nd Gen 上以错误的位置和视角显示对象?

android - OutputStreamWriter 的 flush 方法在尝试写入汉字时抛出 IOException

java - Matlab 将文件从 Windows 资源管理器拖放到图形 (gui)

java - Activiti中的异步流程执行

android - 如何在 ANDROID 的 HttpURLConnection 中使用 Cookie?

java - 模拟 HTTPSURLConnection 类抛出 java.lang.AbstractMethodError

java - Android 手机作为实时 MJPEG 视频服务器

html - multipart/x-mixed-replace 内容类型的视频流问题

node.js - 在 Node.js 中发出 HTTP 请求并接收 multipart/x-mixed-replace 响应

java - android/java在项目中组织http web服务调用的结构/模式是什么?