zoukankan      html  css  js  c++  java
  • HTTP中KeepAlive(长连接)的一些说明

    Http Keep-Alive seems to be massively misunderstood. Here's a short description of how it works, under both 1.0 and 1.1

    HTTP/1.0

    Under HTTP 1.0, there is no official specification for how keepalive operates. It was, in essence, tacked on to an existing protocol. If the browser supports keep-alive, it adds an additional header to the request:

    ConnectionKeep-Alive

    Then, when the server receives this request and generates a response, it also adds a header to the response:

    ConnectionKeep-Alive

    Following this, the connection is NOT dropped, but is instead kept open. When the client sends another request, it uses the same connection. This will continue until either the client or the server decides that the conversation is over, and one of them drops the connection.

    HTTP/1.1

    Under HTTP 1.1, the official keepalive method is different. All connections are kept alive, unless stated otherwise with the following header:

    Connection: close

    The ConnectionKeep-Alive header no longer has any meaning because of this.

    Additionally, an optional Keep-Alive: header is described, but is so underspecified as to be meaningless. Avoid it.

    Not reliable

    HTTP is a stateless protocol - this means that every request is independent of every other. Keep alive doesn’t change that. Additionally, there is no guarantee that the client or the server will keep the connection open. Even in 1.1, all that is promised is that you will probably get a notice that the connection is being closed. So keepalive is something you should not write your application to rely upon.

    KeepAlive and POST

    The HTTP 1.1 spec states that following the body of a POST, there are to be no additional characters. It also states that "certain" browsers may not follow this spec, putting a CRLF after the body of the POST. Mmm-hmm. As near as I can tell, most browsers follow a POSTed body with a CRLF. There are two ways of dealing with this: Disallow keepalive in the context of a POST request, or ignore CRLF on a line by itself. Most servers deal with this in the latter way, but there's no way to know how a server will handle it without testing.

  • 相关阅读:
    【LoadRunner-Vuser Generator】录制脚本设置Recording Options
    【LoadRunner-内部结构】
    【LoadRunner-工作过程】
    单片机上内存管理(重定义malloc free)的实现
    stm32模块的初始化顺序要求的更改设值
    [CAN波形分析] 一次CAN波形分析之旅
    w5500调试小记
    keil mdk中save和load指令,在调试中比较有用,以及hex格式的学习
    PHP启用session后抛 session_start(): open(/var/lib/php/session/sess_... 的解决办法
    brew 方式安装的php,关闭与重启----mac启动,关闭php-fpm方式
  • 原文地址:https://www.cnblogs.com/walkerwang/p/2342703.html
Copyright © 2011-2022 走看看