Web客户端不允许对打开维基百科页面? [英] WebClient forbids opening wikipedia page?
问题描述
下面是我试图运行的代码:
Here's the code I'm trying to run:
var wc = new WebClient();
var stream = wc.OpenRead(
"http://en.wikipedia.org/wiki/List_of_communities_in_New_Brunswick");
不过,我不断收到一个403 Forbidden错误。不明白为什么。它工作得很好了其他页面。我可以在浏览器中打开网页罚款。我该如何解决这个问题?
But I keep getting a 403 forbidden error. Don't understand why. It worked fine for other pages. I can open the page fine in my browser. How can I fix this?
推荐答案
我一般不会使用打开读取()
,试试 DownloadData()
或 DownloadString()
代替。
I wouldn't normally use OpenRead()
, try DownloadData()
or DownloadString()
instead.
此外,它可能是由于你没有提供的用户代理字符串维基百科是故意阻碍您的要求:
Also it might be that wikipedia is deliberately blocking your request because you have not provided a user agent string:
WebClient client = new WebClient();
client.Headers.Add("user-agent",
"Mozilla/5.0 (Windows; Windows NT 5.1; rv:1.9.2.4) Gecko/20100611 Firefox/3.6.4");
我用WebClient的很多时候,并了解到很快的网站能够而且将会阻止你的要求,如果你不T提供相匹配的已知的Web浏览器的用户代理字符串。此外,如果你弥补自己的用户代理字符串(如我的超爽网页刷屏),你也将被阻止。
I use WebClient quite often, and learned quite quickly that websites can and will block your request if you don't provide a user agent string that matches a known web browser. Also, if you make up your own user agent string (eg "my super cool web scraper") you will also be blocked.
我改变了我的例子用户代理字符串到一个现代版的Firefox。我给原来的例子是对IE6的用户代理字符串这不是一个好主意。为什么?某些网站可能基于IE6进行筛选,并与浏览器发送任何信息或到不同的页面,上面写着请更新您的浏览器。 - 这意味着你不会得到你想要得到的内容
I changed my example user agent string to that of a modern version of Firefox. The original example I gave was the user agent string for IE6 which is not a good idea. Why? Some websites may perform filtering based on IE6 and send anyone with that browser a message or to a different page that says "Please update your browser" - this means you will not get the content you wanted to get.
这篇关于Web客户端不允许对打开维基百科页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!