最新消息:20210917 已从crifan.com换到crifan.org

【已解决】HTTP中如何设置连接Connection为不保持不是keep-alive

设置 crifan 654浏览 0评论
折腾:
【未解决】如何破解大众点评网页爬取时的反扒验证verify.meituan.com
期间,对于之前某个地方提到的,注意对于代理IP,不要设置:
"Connection": "keep-alive"
避免连接保持,导致IP不是每次都变化
此处打算注释掉:
# "Connection": "keep-alive",
突然想到:
去找找,是否有专门设置,不keep-alive的
Connection not keep alive
Keep-Alive – HTTP | MDN
Connection – HTTP | MDN
“close
表明客户端或服务器想要关闭该网络连接,这是HTTP/1.0请求的默认值”
HTTP Keep-Alive模式 – 吴秦 – 博客园
HTTP协议头部与Keep-Alive模式详解 – BYVoid
“http 1.0中默认是关闭的,需要在http头加入”Connection: Keep-Alive”,才能启用Keep-Alive;http 1.1中默认启用Keep-Alive,如果加入”Connection: close “,才关闭。目前大部分浏览器都是用http1.1协议,也就是说默认都会发起Keep-Alive的连接请求”
http connection close
HTTP 头 Connection=close 作用 – legend_x的专栏 – CSDN博客
关于设置http响应头connection的作用 – 小生学艺 – ITeye博客
“在http1.1中request和reponse header中都有可能出现一个connection头字段,此header的含义是当client和server通信时对于长链接如何进行处理。
    在http1.1中,client和server都是默认对方支持长链接的, 如果client使用http1.1协议,但又不希望使用长链接,则需要在header中指明connection的值为close;如果server方也不想支持长链接,则在response中也需要明确说明connection的值为close.
    不论request还是response的header中包含了值为close的connection,都表明当前正在使用的tcp链接在请求处理完毕后会被断掉。以后client再进行新的请求时就必须创建新的tcp链接了。 HTTP Connection的 close设置允许客户端或服务器中任何一方关闭底层的连接双方都会要求在处理请求后关闭它们的TCP连接。”
HTTP/1.1: Header Field Definitions
“14.10 Connection
The Connection general-header field allows the sender to specify options that are desired for that particular connection and MUST NOT be communicated by proxies over further connections.
The Connection header has the following grammar:
       Connection = “Connection” “:” 1#(connection-token)
       connection-token  = token
HTTP/1.1 proxies MUST parse the Connection header field before a message is forwarded and, for each connection-token in this field, remove any header field(s) from the message with the same name as the connection-token. Connection options are signaled by the presence of a connection-token in the Connection header field, not by any corresponding additional header field(s), since the additional header field may not be sent if there are no parameters associated with that connection option.
Message headers listed in the Connection header MUST NOT include end-to-end headers, such as Cache-Control.
HTTP/1.1 defines the “close” connection option for the sender to signal that the connection will be closed after completion of the response. For example,
       Connection: close
in either the request or the response header fields indicates that the connection SHOULD NOT be considered `persistent’ (section 8.1) after the current request/response is complete.
HTTP/1.1 applications that do not support persistent connections MUST include the “close” connection option in every message.
A system receiving an HTTP/1.0 (or lower-version) message that includes a Connection header MUST, for each connection-token in this field, remove and ignore any header field(s) from the message with the same name as the connection-token. This protects against mistaken forwarding of such header fields by pre-HTTP/1.1 proxies. See section 19.6.2.”
4.7. The Mysteries of Connection Close – HTTP: The Definitive Guide [Book]
【总结】
Connection的值有2种:
  • close
  • keep-alive
    • 保持连接=持久连接=持续连接=连接重用
区别:
对于如何确保关闭连接:
  • http 1.0
    • 默认:
      • Connection: close
  • http 1.1
    • 默认:
      • Connection: keep-alive
    • 想要不保持连接=不持续连接:
      • 主动设置为关闭:
        • Connection: close
      • 不能:只是不传递,只是注释掉:
        • # Connection: keep-alive

转载请注明:在路上 » 【已解决】HTTP中如何设置连接Connection为不保持不是keep-alive

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
91 queries in 0.190 seconds, using 23.38MB memory