HttpWebResponse - 操作超时

时间:2017-11-12 21:12:38

标签: c# wpf

我正在尝试下载并保存各种网站的图标。对于大多数,以下代码有效。但是,我对一些网址有疑问。例如:

https://www.bestbuy.com/favicon.ico bestbuyhttps://www.macys.com/favicon.ico macys

我可以在我的默认浏览器(firefox)中打开这些网址,没有任何问题。

这是我用来执行HttpWebRequest以及我获得例外的代码。

这就是我执行WebRequest的方式

HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
request.Timeout = 10000;
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Headers.Add("Upgrade-Insecure-Requests", "1");
request.CookieContainer = new CookieContainer();
request.UserAgent = "Application name here";
response = request.GetResponse() as HttpWebResponse;

任何想法为什么示例网址超时(再次,大多数工作正常)。 `

1 个答案:

答案 0 :(得分:0)

您被使用者阻止了。发送浏览器发送的内容。我用过这个:

Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36

HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
request.Timeout = 10000;
request.AutomaticDecompression = DecompressionMethods.GZip | 
DecompressionMethods.Deflate;
request.Headers.Add("Upgrade-Insecure-Requests", "1");
request.CookieContainer = new CookieContainer();
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36";
response = request.GetResponse() as HttpWebResponse;