gpt4 book ai didi

http - Http Session 和 Http Stateless 矛盾吗?

转载 作者:可可西里 更新时间:2023-11-01 17:09:13 26 4
gpt4 key购买 nike

众所周知,HTTP 是无状态的。这意味着,服务器端不会存储有关客户端的任何信息。但是我们在用C#写代码的时候,可能会使用Http Session来为用户客户端存储一些数据,比如一个网店应用的订单信息。这矛盾吗?

我想,一个解释可能是stateless这个词是给Each Connection的。每次它收到一个请求,当响应发出后,连接就会关闭并销毁。

你们能告诉我为什么这不矛盾吗?谢谢。

待更新信息:

核心 HTTP 协议(protocol)本身是无状态的(如果忽略持久连接、缓存等),但是实现 HTTP 的服务器以及构建在 HTTP 之上的 Web 应用程序不一定是无状态的

最佳答案

As we all know, HTTP is stateless. That means, the server side will not store any information about the client side.

核心 HTTP 协议(protocol)本身是无状态的(如果您忽略持久连接、缓存等),但是实现 HTTP 的服务器和构建在 HTTP 之上的 Web 应用程序不一定是您所说的无状态。实际上,有用于传递 session 标识信息的相关标准;例如使用 cookie。而且您甚至不需要 cookie。 session token 可以作为 URL 的一部分传递...

请提供一个或多个来源的链接,使您相信 HTTP 服务器不会存储有关客户端的信息......以便我们找到您误解的根源。


I think, a explanation may be that the word stateless is to Each Connection. Every time it gets a request, and when the response is send out, the connection is closed and destroyed.

这或多或少是正确的。 (对于持久连接,发送响应时连接不会被破坏。)

但这并不以任何方式支持您关于 session 与 HTTP 无状态相矛盾的断言。 session 存在于核心 HTTP 协议(protocol)之上的概念层面。


也许您误解的真正根源是术语无状态协议(protocol)。根据Wikipedia :

"In computing, a stateless protocol is a communications protocol in which no session information is retained by the receiver, usually a server. Relevant session data is sent to the receiver by the client in such a way that every packet of information transferred can be understood in isolation, without context information from previous packets in the session."

从这个意义上说,HTTP 协议(protocol)是无状态的。一个 HTTP 请求和响应可以独立于另一个请求/响应来理解。基本 HTTP 不依赖于 session 。 (相比之下,TCP 协议(protocol)从根本上依赖于跨多个数据包跟踪序列号。)

Wikipedia article然后这样说:

"There can be complex interactions between stateful and stateless protocols among different protocol layers. For example, HTTP is an example of a stateless protocol layered on top of TCP, a stateful protocol, which is layered on top of IP, another stateless protocol, which is routed on a network that employs BGP, another stateful protocol, to direct the IP packets riding on the network."

"This stacking of layers continues even above HTTP. As a workaround for the lack of a session layer in HTTP, HTTP servers implement various session management methods, typically utilizing a unique identifier in the cookie or parameter that allows the server to track requests originating from the same client, and effectively creating a stateful protocol on top of HTTP.".

这意味着“HTTP session ”(例如,通过 cookie 实现)是 HTTP 协议(protocol)层之上的协议(protocol)层。因此,这并不与 HTTP 协议(protocol)的无状态性“矛盾”。

关于http - Http Session 和 Http Stateless 矛盾吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26322003/

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