cookie和cookiejar之间有什么区别?

huangapple go评论85阅读模式
英文:

What is the difference between cookie and cookiejar?

问题

今天我遇到了术语"cookiejar"(包net/http/cookiejar)。我试图收集一些关于它的信息,但没有得到任何明晰的结果。我知道cookie是服务器发送给客户端的键值对,例如:Set-Cookie: foo=10,浏览器会将其存储在本地,然后在每个后续请求中将这些cookie发送回服务器,例如:Cookie: foo=10

好的,那么cookiejar是什么,它是什么样子的?

英文:

Today I faced the term "cookiejar" (package net/http/cookiejar). I tried to gather some information regarding it, but got nothing intelligible came out. I know that cookie is key/value pairs that server sends to a client, eg: Set-Cookie: foo=10, browser stores it locally and then each subsequent request browser will send these cookies back to the server, eg: Cookie: foo=10.

Ok, but what about cookiejar? What is it and how does it look like?

答案1

得分: 66

根据你的问题描述,cookie由浏览器(HTTP客户端)管理,它们允许在客户端的计算机上存储信息,并在后续请求中由浏览器自动发送。

如果你的应用程序充当客户端(使用net/http包连接到远程HTTP服务器),那么没有浏览器来处理/管理cookie。我的意思是,没有浏览器来存储/记住作为Set-Cookie:响应头部到达的cookie,并将它们附加到发送到同一主机/域的后续请求中。此外,cookie还有一个过期日期,在决定是否将其包含在发送的请求中之前,你也需要检查它们。

然而,http.Client类型允许你设置一个http.CookieJar类型的值,如果你这样做,你将拥有自动的cookie管理,否则你将不会有或者你将不得不自己处理它。这使你可以使用net/http包进行多个请求,服务器将把它们视为同一会话的一部分,就像它们是由真实的浏览器发出的一样,因为通常使用cookie来维护HTTP会话(会话ID)。

net/http/cookiejar包是一个可直接使用的CookieJar实现。请注意,这个实现只存在于内存中,这意味着如果你重新启动应用程序,cookie将会丢失。

总的来说,HTTP cookie是从网站发送并存储在用户的Web浏览器中的一小段数据,而用户正在浏览该网站。

CookieJar是一个简单的cookie管理器的Go接口(用于管理来自HTTP请求和响应头的cookie),以及该接口的实现

英文:

As you described in your question, cookies are managed by browsers (HTTP clients) and they allow to store information on the clients' computers which are sent automatically by the browser on subsequent requests.

If your application acts as a client (you connect to remote HTTP servers using the net/http package), then there is no browser which would handle / manage the cookies. By this I mean storing/remembering cookies that arrive as Set-Cookie: response headers, and attaching them to subsequent outgoing requests being made to the same host/domain. Also cookies have expiration date which you would also have to check before deciding to include them in outgoing requests.

The http.Client type however allows you to set a value of type http.CookieJar, and if you do so, you will have automatic cookie management which otherwise would not exist or you would have to do it yourself. This enables you to do multiple requests with the net/http package that the server will see as part of the same session just as if they were made by a real browser, as often HTTP sessions (the session ids) are maintained using cookies.

The package net/http/cookiejar is a CookieJar implementation which you can use out of the box. Note that this implementation is in-memory only which means if you restart your application, the cookies will be lost.


So basically an HTTP cookie is a small piece of data sent from a website and stored in a user's web browser while the user is browsing that website.

Cookiejar is a Go interface of a simple cookie manager (to manage cookies from HTTP request and response headers) and an implementation of that interface.

答案2

得分: 3

一般来说,它是一个数据存储区,应用程序(无论是浏览器还是其他应用)在请求和响应期间使用的 cookie 都会放在其中。因此,它实际上是一个用于存放 cookie 的罐子。

英文:

In general it is a datastore where an application (browser or not) puts the cookies it uses during requests and responses. So it is really a jar for cookies.

huangapple
  • 本文由 发表于 2015年7月7日 21:50:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/31270461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定