问题描述
我有一个返回网页内容的方法:
I have a method which returns the content of a webpage:
private string FetchHTML(string sUrl, Encoding encoding)
{
System.Net.WebClient oClient = new System.Net.WebClient();
oClient.Encoding = encoding;
return System.Web.HttpUtility.HtmlDecode(oClient.DownloadString(sUrl));
}
但是,当我尝试加载从的LiveJournal(链接例如,)然后我得到这个例外在DownloadString:
But when I try to load a link from livejournal (for instance, http://mos-jkh.livejournal.com/769579.html) then I am getting this exception at DownloadString:
请求已中止:操作已超时
它是一个已知的问题?为什么不DownloadString工作的一些网页,是有一个解决方案?或者是有到DownloadString替代?
Is it a known issue? Why doesn't DownloadString work for some webpages and is there a solution for this? Or is there an alternative to DownloadString?
推荐答案
有些网站有足够的智慧来检查请求是否被浏览器或没有作出。而当他们发现该请求与不应诉浏览器没有这样做。但它很容易,只需发送带有请求的用户代理信息来糊弄他们。因此,溶液中加入code的一个单线路FetchHTML方法:
Some websites are smart enough to check whether the request is made by a browser or not. And when they detect that the request was done not with a browser they don't respond. But it's easy to fool them by simply sending the user agent info with the request. So the solution was adding one single line of code to the FetchHTML method:
private string FetchHTML(string sUrl, Encoding encoding)
{
System.Net.WebClient oClient = new System.Net.WebClient();
oClient.Encoding = encoding;
// set the user agent to IE6
oClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)");
return System.Web.HttpUtility.HtmlDecode(oClient.DownloadString(sUrl));
}
PS:检测我是用代替Wireshark的,我已经找到了过于复杂问题。
PS: To detect the issue I was using Fiddler instead of Wireshark which I've found too complex.
这篇关于DownloadString超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!