Microsoft Azure 应用程序网关阻止 python 的请求库

Microsoft Azure Application Gateway blocking python's requests library

我正在尝试使用 Python 登录到 Azure 中托管的 API 并且 requests 是我用于 http 请求的首选库。试过这个:

import requests

url = f"https://{SERVER}/{PLATFORM}/Account/Logon"
payload = f'LicenseType=&Password={PASSWORD}&RedirectToMyselfInCaseOfError=false&RememberMe=false&UserName={USERNAME}'
headers = {
  'Content-Type': 'application/x-www-form-urlencoded',
  'Accept': 'application/json',
}

response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

但得到了:

<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>Microsoft-Azure-Application-Gateway/v2</center>
</body>
</html>

如果我使用另一个 python 库,例如 http.client,它会很好地工作:

import http.client

conn = http.client.HTTPSConnection(SERVER)
payload = f'LicenseType=&Password={PASSWORD}&RedirectToMyselfInCaseOfError=false&RememberMe=false&UserName={USERNAME}'
headers = {
  'Content-Type': 'application/x-www-form-urlencoded',
  'Accept': 'application/json',
}
conn.request("POST", f"/{PLATFORM}/Account/Logon", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

有什么方法可以避免 Azure 将我的“请求”库请求标记为恶意?

找到原因:Curl and Python Requests (get) reporting different http status code

只需将 User-Agent 字段添加到 headers