Python 请求 POST 命令不遵循 302 重定向

问题描述 投票:0回答:3

通过请求将 POST 请求传递到网站时,无法完成并且将无限期等待。

我已经查看了 Fiddler 中的行为,可以看到 POST 请求通过

302
重定向成功返回。然而,尽管数据包已返回,但请求仍会继续等待并且永远不会继续。

我被困在如何诊断这个问题上,因为我没有得到任何可靠的错误,并且数据包似乎返回,任何帮助将不胜感激。

post请求构建如下:

loginData = 'data=45B892A0F9C127FB0A052CB&cid=1000'
s.post(url, verify=rVerify, headers=headers, data=loginData)

我尝试过将allow_redirects设置为True和False,但没有成功。

以下是 Fiddler 中的请求和响应:

fiddler result

更新标题详细信息:

'User-Agent': 'Mozilla/5.0'
'Content-Type': 'application/x-www-form-urlencoded'
'Connection': 'Keep-Alive'
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
'Accept-Language': 'en-US,en;q=0.5'
'Accept-Encoding': 'gzip, deflate, br'

更新:

  • 我只使用 URL 进行了测试,命令仍然会挂起。
  • 我也尝试过 GET 请求,但这也挂起。
python python-3.x post https python-requests
3个回答
0
投票

您可以尝试使用 POST 对象手动重定向 302 返回的下一个 URL。

loginData = 'data=45B892A0F9C127FB0A052CB&cid=1000'
response = s.post(url, verify=rVerify, headers=headers, data=loginData)

next_url = response['location']

# do your next step here using the returned URL

302 应该返回一个 URL 以在标头中重定向到“Location”的值。如上所示,您将在响应对象字典中找到它。

来源:https://en.wikipedia.org/wiki/HTTP_302https://2.python-requests.org/en/master/api/#requests.Response


0
投票

所以问题似乎出在 Python3 和 httplib 库无法解析从服务器返回的标头。

这是问题:https://github.com/kennethreitz/requests/issues/3098

为了解决这个问题,我刚刚更改为 Python 2,它不需要以相同的方式解析标头。


0
投票

在这里查看我的答案https://stackoverflow.com/a/78845896/364818

简单地说,

requests
被故意破坏,在重定向后将 POST 转换为 GET,以模仿浏览器被破坏的方式...

© www.soinside.com 2019 - 2024. All rights reserved.