zoukankan      html  css  js  c++  java
  • requests 发送请求时 保持 headers 的顺序

    如题,在使用 requests 发送 post 请求时保持 headers 的顺序不变

    问题来源于 http://match.yuanrenxue.com/match/3 这个网站
    在抓包时发现每次获取数据前都会发送一个 post 请求去获取cookies

    在模拟时却发现怎么都无法获取cookies
    通过 fiddler 查看发现是 headers 的顺序有问题,
    通过 postman 调整 headers 的顺序,模拟发送请求,确定是 headers 顺序的问题

    问题定位

    通过 fiddler 查看发现可能是 headers 的顺序有问题,
    通过 postman 调整 headers 的顺序,模拟发送请求,确定是 headers 顺序的问题

    错误的响应

    正确的响应

    解决方案

    import requests
    
    url = "xxx"
    headers = {
        "Host": "match.yuanrenxue.com",
        "Connection": "keep-alive",
        "Content-Length": "0",
        "Origin": "http://match.yuanrenxue.com",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
        "Accept": "*/*",
        "Referer": "http://match.yuanrenxue.com/match/3",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9",
    }
    # 先session
    session=requests.Session()
    # 后session clear,clear这个是比较关键的写法。
    session.headers.clear()
    session.headers.update(
        headers
    )
    resp=session.post(url)
    

    谨记

    之前从未遇到过这个问题,

  • 相关阅读:
    python内置函数
    conda和anaconda的区别
    闭包,装饰器,property
    【模板】大数乘法(51nod 1027)
    51nod 1791 合法括号子段
    51nod 1419 最小公倍数挑战
    51nod 1241 特殊的排序
    51nod 1090 3个数和为0
    【模板】51nod 1051 最大子矩阵和
    51nod 1267 4个数和为0
  • 原文地址:https://www.cnblogs.com/hanyanling/p/13845850.html
Copyright © 2011-2022 走看看