System.Net.WebClient 请求收到 403 Forbidden,但浏览器在 Apache 服务器上却没有
奇怪的是,我正在尝试阅读 部分。许多不同网站的部分,以及一种特定类型的服务器 Apache,有时会给出代码 403 禁止。并非所有 apache 服务器都这样做,因此它可能是配置设置或服务器的特定版本。
然后,当我使用网络浏览器(例如 Firefox)检查 url 时,页面加载正常。代码看起来像这样:
var client = new WebClient();
var stream = client.OpenRead(new Uri("http://en.wikipedia.org/wiki/Barack_Obama"));
通常,403 是访问权限失败之类的事情,但这些通常是不安全的页面。我认为 Apache 正在过滤请求标头中的某些内容,因为我不想创建任何内容。
也许对 Apache 有更多了解的人可以给我一些关于标头中缺少的内容的想法。我希望标头尽可能小,以最大限度地减少带宽。
谢谢
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.
When I then check the url with a web browser (Firefox, for example) the page loads fine. The code sorta looks like this:
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.
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.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试设置 UserAgent 标头:
Try setting the UserAgent header:
我遇到了类似的问题,下面的设置解决了它
I had a similar problem and below setting solved it
正如“thedugas”所说,这可能是 UserAgent 标头的问题,或者实际上是浏览器默认配置要做的任何事情。例如,可能是未使用浏览器正在使用的代理服务器,或者未使用代理服务器的正确凭据。这些事情可能已经配置到浏览器中,因此您不知道需要完成它们。
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.
我有同样的问题,答案并不明显。我找到了嗅探网络通信的解决方案。当 Apache 给出“Testing 1 2 3...”页面时,它会返回一个带有 403 禁止代码的 html。浏览器忽略获取代码并显示页面,但 de WebClient 返回错误消息。解决方案是读取 Try 语句的 Catch 内的响应。这是我的代码:
在 Try 语句之后,Retorno 将包含来自服务器的 HTML 响应,无论服务器返回什么错误代码。
标头对此行为没有影响。
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:
After the Try statment, Retorno will contain the HTML response from the server, no matter the error code the server returns.
The headers have no influence on this behaiviour.