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)
    

    谨记

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

  • 相关阅读:
    7-30-组队赛
    POJ 3125 Printer Queue
    7-28-比赛
    POJ 3922 A simple stone game
    POJ 1845
    第一次组队训练
    I-number
    Radar Installation
    Robots on a grid(DP+bfs())
    Dividing a Chocolate(zoj 2705)
  • 原文地址:https://www.cnblogs.com/hanyanling/p/13845850.html
Copyright © 2011-2022 走看看