python 代码的问题。一个请求浏览器中发起是 http2 协议的,能成功请求,但是抓到 postman 或者放到代码中发起请求的时候是 http1.1 的,服务器给我返回 403 了。下面的代码看似是发起了 http2 请求,但实际上还是返回了 403,不知道哪里的问题。这个请求经过 fiddler 代理的时候又能成功。
import json
import httpx
client = httpx.Client(http2=True)
url = "https://api.dhldanhao.com/waybill/listForCodes"
payload = {
"page_size": "",
"begin_time": "1722960000",
"end_time": "",
"types": "USPS",
"codes": "41035",
"sn_status": "1",
"start_address": ""
}
headers = {
'accept': 'application/json, text/plain, */*',
'accept-language': 'zh-CN,zh;q=0.9',
'access-control-allow-origin': '*',
'content-type': 'application/json',
'origin': 'https://www.dhldanhao.com',
'priority': 'u=1, i',
'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'
}
response = client.request("POST", url, headers=headers, content=json.dumps(payload))
print(response.http_version)
print(response.status_code)