System.Net.WebClient 请求得到 403 Forbidden 但浏览器没有使用 Apache 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2316111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.Net.WebClient request gets 403 Forbidden but browsers do not with Apache servers
提问by Grant BlahaErath
An odd one, I'm trying to read the <Head> section of a lot of different websites out there, and one particular type of server, Apache, sometimes gives the code 403 forbidden. Not all apache servers do this, so it may be a config setting or a particular version of the server.
奇怪的是,我正在尝试阅读许多不同网站的 <Head> 部分,而一种特定类型的服务器 Apache 有时会禁止使用 403 代码。并非所有 apache 服务器都这样做,因此它可能是配置设置或服务器的特定版本。
When I then check the url with a web browser (Firefox, for example) the page loads fine. The code sorta looks like this:
然后,当我使用 Web 浏览器(例如 Firefox)检查 url 时,页面加载正常。代码大概是这样的:
var client = new WebClient();
var stream = client.OpenRead(new Uri("http://en.wikipedia.org/wiki/Barack_Obama"));
Normally, a 403 is a access permission failed sort of thing, but these are normally unsecure pages. I'm thinking that Apache is filtering on something in the request headers since I'm not bothering to create any.
通常,403 是访问权限失败之类的事情,但这些通常是不安全的页面。我认为 Apache 正在过滤请求标头中的某些内容,因为我不想创建任何内容。
Maybe someone who knows more about Apache can give me some ideas of what's missing in the headers. I'd like to keep the headers as small as possible to minimize bandwidth.
也许对 Apache 有更多了解的人可以给我一些关于标题中缺少的内容的想法。我想保持标题尽可能小以最小化带宽。
Thanks
谢谢
回答by dugas
Try setting the UserAgent header:
尝试设置 UserAgent 标头:
string _UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
client.Headers.Add(HttpRequestHeader.UserAgent, _UserAgent);
回答by Anoop
I had a similar problem and below setting solved it
我有一个类似的问题,下面的设置解决了它
Client.Headers["Accept"] = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
Client.Headers["User-Agent"] ="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDC)";
回答by John Saunders
It could be a matter of the UserAgent header, as "thedugas" said, or in fact anything the browser is silently configured to do. For instance, it could be a matter of not using a proxy server that the browser is using, or not using the correct credentials for the proxy server. These are things that may already be configured into the browser, so you're not aware they need to be done.
正如“thedugas”所说,这可能是 UserAgent 标头的问题,或者实际上浏览器被静默配置的任何内容。例如,可能是因为没有使用浏览器正在使用的代理服务器,或者没有使用代理服务器的正确凭据。这些可能已经配置到浏览器中,因此您不知道它们需要完成。
回答by Carlos Eduardo Foltran
I had the same problem and the answer was not obvious. I found the solution sniffing the network communication. When Apache gives its "Testing 1 2 3..." page, it returns an html with a 403 forbiden code. The browser ignores gets the code and show the page, but de WebClient returns an error message. The solution is to read the response inside the Catch of a Try statment. Here is my code:
我有同样的问题,答案并不明显。我找到了嗅探网络通信的解决方案。当 Apache 提供其“Testing 1 2 3...”页面时,它返回一个带有 403 禁止代码的 html。浏览器忽略获取代码并显示页面,但 de WebClient 返回错误消息。解决方案是读取 Try 语句的 Catch 中的响应。这是我的代码:
Dim Retorno As String = ""
Dim Client As New SiteWebClient
Client.Headers.Add("User-Agent", "Mozilla/ 5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " &
"(KHTML, Like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134")
Client.Headers.Add("Accept-Language", "pt-BR, pt;q=0.5")
Client.Headers.Add("Accept", "Text/ html, application / xhtml + Xml, application / Xml;q=0.9,*/*;q=0.8")
Try
Retorno = Client.DownloadString("http://" & HostName & SitePath)
Catch ex As Exception
If ex.GetType = GetType(System.Net.WebException) Then
Try
Dim Exception As System.Net.WebException = ex
Dim Resposta As System.Net.HttpWebResponse = Exception.Response
Using WebStream As New StreamReader(Resposta.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"))
Retorno = WebStream.ReadToEnd
End Using
Catch ex1 As Exception
End Try
End If
End Try
After the Try statment, Retorno will contain the HTML response from the server, no matter the error code the server returns.
在 Try 语句之后,Retorno 将包含来自服务器的 HTML 响应,无论服务器返回什么错误代码。
The headers have no influence on this behaiviour.
标题对这种行为没有影响。

