使用 WebClient 时出现奇怪的 403 错误

Odd 403 error using WebClient

我正在尝试发送 GET 请求以下载 http://footlocker.com/ 的 HTML 内容:

Console.WriteLine(new WebClient().DownloadString("http://footlocker.com"));

但是我收到 403 错误。为了测试,我使用 Python 尝试发送 GET 请求(请求库),我成功收到了 200 响应以及 HTML 内容:

r = requests.get('http://footlocker.com')
print(r.text)

为了查看区别,我在 Python 请求中打印了 headers,这就是我得到的:

{'User-Agent': 'python-requests/2.13.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}

所以我尝试使用 Python 请求 User-Agent 字符串发送 WebClient 请求:

WebClient wc = new WebClient();
wc.Headers[HttpRequestHeader.UserAgent] = "python-requests/2.13.0";
Console.WriteLine(wc.DownloadString("http://footlocker.com"));

但是我还是得到了403。Python的请求库和WebClient有什么区别?我在这里遗漏了一些明显的东西吗?为什么会这样?

想通了,需要添加这个 header:

wc.Headers.Add("Accept", "*/*");

最终代码:

WebClient wc = new WebClient();
wc.Headers.Add("User-Agent", "python-requests/2.13.0");
wc.Headers.Add("Accept", "*/*");
Console.WriteLine(wc.DownloadString("http://footlocker.com"));