In What conditions are closing a HTTP connection necessary?

限于喜欢 提交于 2019-12-05 10:11:59

问题


In What conditions are closing a HTTP connection necessary?


回答1:


HTTP isn't the type of protocol to have "connections"; it's what they call "stateless", meaning each request is separate from every other request. That's why we have things like session cookies; people had to hack in a way to allow information to be carried over between requests.

Now, even though they're separate, HTTP 1.1 allows a client to make multiple requests over the same TCP/IP connection (which, although it's a connection to an HTTP server, is at a whole other level in the TCP/IP stack). The requests will still be separate, but you don't have to open a new network connection. This allows some efficiency gains, as opening a network connection can be expensive.

If you'd like to take advantage of this, watch the headers in the request and response. If the request is using an HTTP version less than 1.1, or there's a header that says Connection: close, then the connection is to go away after the current request is handled. Otherwise, once it's been cleared out (usually by reading all the data from the previous request), it can be reused.



来源:https://stackoverflow.com/questions/3272653/in-what-conditions-are-closing-a-http-connection-necessary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!