简体   繁体   English

如何获取请求 cookie 而不仅仅是响应 cookie

[英]How to get request cookies and not just response cookies

I'm sending requests to a website that uses cookies to keep track of the session.我正在向使用 cookie 跟踪会话的网站发送请求。 The confusing part to me is that when I go to the logon page and inspect, chrome SENDS cookies first?令我困惑的部分是,当我进入登录页面并检查时,chrome 首先发送 cookie? This is before I login.这是我登录之前。 Shouldn't I receive a cookie after I login?我登录后不应该收到 cookie 吗? This is the website: https://unionline.uniongas.com/ul/Account/LogOn这是网站: https : //unionline.uniongas.com/ul/Account/LogOn

You can see in the request cookies there's four cookies: "ULTICKET", "ULRMUID" and "ULTOKEN".您可以在请求 cookie 中看到有四个 cookie:“ULTICKET”、“ULRMUID”和“ULTOKEN”。 How is google sending those to the server without having to fetch them first?谷歌如何将这些发送到服务器而不必先获取它们?

And when I send a request to https://unionline.uniongas.com/ul/Account/LogOn I only receive the "ULTOKEN" cookie and not the other two.当我向https://unionline.uniongas.com/ul/Account/LogOn发送请求时,我只收到“ULTOKEN”cookie 而不是其他两个。

My code:我的代码:

 s = requests.Session()
        
    headers = {
    'Connection': 'keep-alive',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Sec-Fetch-Site': 'none',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-User': '?1',
    'Sec-Fetch-Dest': 'document',
    'Accept-Language': 'en-US,en;q=0.9',
    }

    response = s.get('https://unionline.uniongas.com/ul/Account/LogOn', headers=headers)
    print(s.cookies.get_dict())
    

Which prints:哪个打印:

{'ULTOKEN': ******}

How do I actually get all the request cookies with Python?我如何实际使用 Python 获取所有请求 cookie?

Thanks谢谢

I cannot comment yet on this site so I have no choice but to present it as an answer and perhaps it will even suffice, who knows, because you actually ask two questions here.我还不能在这个网站上发表评论,所以我别无选择,只能将它作为答案呈现,也许它甚至就足够了,谁知道呢,因为你实际上在这里问了两个问题。

What I want to say if it is not the case that you have already had these cookies from previous interactions with the site?如果您之前与网站的交互中没有这些 cookie,我想说些什么?

I connected to this address first time in my life and the browser did not send any cookies in its first request but it did send ULTOKEN later on.我这辈子第一次连接到这个地址,浏览器在它的第一个请求中没有发送任何 cookie,但它后来确实发送了 ULTOKEN。

Which is as expected because there is no way for a browser to know that a random site expects such and such cookie unless the browser has it somehow hard-coded which naturally I doubt could be the case here.这正如预期的那样,因为浏览器无法知道随机站点需要这样那样的 cookie,除非浏览器以某种方式对其进行了硬编码,我自然怀疑这里可能是这种情况。

As to the getting cookies sent in a request part, does that help?至于在请求部分发送 cookie,这有帮助吗?

print(response.request.cookies)

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

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