Python请求post收到错误的重定向URL

2024-10-01 00:15:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在登录我们院长的网站。但是我在通过Python请求发布数据时收到了一个错误。在用Chrome检查这个过程之后,我发现方法POST接收到的URL与Chrome接收到的不同。你知道吗

这是我的部分代码。你知道吗

import requests
url_get = 'http://ssfw.xjtu.edu.cn/index.portal'
url_post = 'https://cas.xjtu.edu.cn/login?service=http%3A%2F%2Fssfw.xjtu.edu.cn%2Findex.portal'

s = requests.session()
user = {"username": email,
        "password": password,
       }

header = {
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Encoding':'gzip, deflate',
    'Accept-Language':'zh-CN,zh;q=0.8',
    'Cache-Control':'max-age=0',
    'Connection':'keep-alive',
    'Content-Length':'141',
    'Content-Type':'application/x-www-form-urlencoded',
    'Host':'cas.xjtu.edu.cn',
    'Origin':'https://cas.xjtu.edu.cn',
    'Upgrade-Insecure-Requests':'1',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36'
    }

我从viaa = s.get(url_get)获得了cookie,它应该重定向到url_post,然后添加cookie和referer。你知道吗

_cookie = a.cookies['JSESSIONID']
header['Cookie'] = 'JSESSIONID='+_cookie
header['Referer']= 'https://cas.xjtu.edu.cn/login;jsessionid='+_cookie+'?service=http%3A%2F%2Fssfw.xjtu.edu.cn%2Findex.portal'
r = s.post(url2, json = user, allow_redirects = False)

但是r.headers['location'] == 'https://cas.xjtu.edu.cn/login?service=http%3A%2F%2Fssfw.xjtu.edu.cn%2Findex.portal'

在Chrome上应该是http://ssfw.xjtu.edu.cn/index.portal?ticket=ST-211860-UEh41PdZXfpg4rsvyDg1-gdscas01

嗯…实际上我想知道为什么它们不同,我怎么能通过Python Requests跳转到正确的URL(似乎Chrome上的那个是正确的)


Tags: httpsurlgetcookieserviceloginchromecn