简体   繁体   中英

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. The confusing part to me is that when I go to the logon page and inspect, chrome SENDS cookies first? This is before I login. Shouldn't I receive a cookie after I login? This is the website: https://unionline.uniongas.com/ul/Account/LogOn

You can see in the request cookies there's four cookies: "ULTICKET", "ULRMUID" and "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.

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?

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?

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.

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.

As to the getting cookies sent in a request part, does that help?

print(response.request.cookies)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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