简体   繁体   English

第一次请求时未发送Cookie

[英]Cookie not getting sent on first request

I am trying to understand cookies and what causes them to be sent as part of a request. 我正在尝试了解Cookie,以及是什么导致它们作为请求的一部分发送的。 My basic understanding of cookies is that they are name value pairs that get sent with every request based on the path variable. 我对Cookie的基本理解是,它们是基于路径变量随每个请求发送的名称/值对。 I am doing a basic test that seems to suggest that cookies are not sent on an initial request. 我正在做一个基本测试,似乎表明在初始请求中未发送cookie。 My test is this. 我的测试是这个。

  1. Open up a chrome incognito mode. 打开Chrome隐身模式。
  2. Press F12, enter document.cookie = "test_value=test_name;path=/;" 按F12,输入document.cookie = "test_value=test_name;path=/;" into the console. 进入控制台。
  3. Go to google.com. 前往google.com。

Now if I look at the network tab and look at the request to google.com. 现在,如果我查看“网络”标签,然后查看对google.com的请求。 Under the cookies tab I can see that no cookies were sent with the request. 在Cookie选项卡下,我可以看到没有随请求发送任何Cookie。 Why not? 为什么不?

在此处输入图片说明

Furthermore, once I am at www.google.com, if repeat steps 2 and 3 in the above procedure, the cookie does get sent this time. 此外,一旦我进入www.google.com,如果重复上述过程中的步骤2和3,则这次会发送Cookie。 Not only does it get sent, I get 4 response cookies. 它不仅被发送,而且得到4个响应cookie。

在此处输入图片说明

So why does the cookie not get sent the first time, and why do I get 4 response cookies the second time? 那么,为什么第一次没有发送Cookie,为什么第二次得到4个响应cookie?

The first page Google Chrome shows when user starts incognito mode is handled differently from the other ones. Google Chrome在用户启动incognito mode时显示的第一页的处理方式与其他页面不同。 It doesn't share browser resources (like cookies storage, ...) with Internet pages. 它不会与Internet页面共享浏览器资源(例如Cookie存储等)。 If you try with another page you'll see that it works correctly. 如果尝试其他页面,则会看到它正常工作。

For example, if you go to www.example.com , type document.cookie = "testCookie=value; path=/" in JavaScript console and refresh the page you'll see the header Cookie containing it in HTML request. 例如,如果您访问www.example.com ,则在JavaScript控制台中键入document.cookie = "testCookie=value; path=/"并刷新页面,您将在HTML请求中看到包含该标题的Cookie

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM