You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

urllib3请求HTTPS接口跳转HTTP报无权限 同参数requests请求正常

问题说明

我编写了如下使用urllib3发起请求的代码:

http = urllib3.PoolManager()
resp = http.request(
    "GET",
    "https://api.nhs.uk/medicines/aciclovir",
    headers={
        "subscription-key":"XXX"
    }
)
print(resp.data)

执行后得到的响应内容为:

You don't have permission to access "http://api.nhs.uk/medicines/aciclovir" on this server

我明确请求的是HTTPS协议地址,不清楚为何会被指向http://开头的地址。

但我使用requests库编写同参数的请求代码如下,却可以请求成功:

url = "https://api.nhs.uk/medicines/aciclovir"

payload={}
headers = {
  'subscription-key': 'XXX'
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

现寻求该问题的产生原因及可行解决方案。

更新1

已确认执行的代码完全正确,确实返回了指向http://地址的无权限报错,相关运行截图见:
运行截图

更新2

分别抓包获取两个库的请求头信息如下:

urllib3请求头

Listening on 0.0.0.0 7777
Connection received on *** 50054
GET / HTTP/1.1
Host: ***:7777
Accept-Encoding: identity
subscription-key: xxx
User-Agent: python-urllib3/1.26.9

requests请求头

Listening on 0.0.0.0 7777
Connection received on *** 50095
GET / HTTP/1.1
Host: ***:7777
User-Agent: python-requests/2.28.0
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive
subscription-key: xxx

内容的提问来源于stack exchange,提问作者pee2pee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 13:15:24