【问题标题】:Too many redirections were attempted using WebRequest使用 WebRequest 尝试了太多重定向
【发布时间】:2015-12-22 16:38:16
【问题描述】:

当尝试抓取网页的 html 时,我偶尔会收到“尝试重定向过多”的异常。

这种网站的一个例子是http://www.magicshineuk.co.uk/

通常我会将超时设置为 6 秒……但即使设置为 30 秒,并且允许的最大重定向数设置为 200 之类的疯狂值,它仍然会抛出“重定向过多”异常,或者,将发生超时。

我该如何解决这个问题?

我的代码在下面...

    try
{

   System.Net.WebRequest request = System.Net.WebRequest.Create("http://www.magicshineuk.co.uk/");

   var hwr = ((HttpWebRequest)request);

   hwr.UserAgent ="Mozilla/5.0 (Windows NT 10.0; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0";
   hwr.Headers.Add("Accept-Language", "en-US,en;q=0.5");
   hwr.Headers.Add("Accept-Encoding", "gzip, deflate");

   hwr.ContentType = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; ;
   hwr.KeepAlive = true;
   hwr.Timeout = 30000;   // 30 seconds...  normally set to 6000
   hwr.Method = "GET";
   hwr.AllowAutoRedirect = true;
   hwr.CookieContainer = new System.Net.CookieContainer();

   // Setting this Makes no difference... normally I would like to keep to a sensible maximum but I will leave as the default of 50 if needs be... 
   // Either way, the Too Many Redirections exception occurs
   hwr.MaximumAutomaticRedirections = 200;   

   using (var response = (HttpWebResponse)hwr.GetResponse())
   {

       Console.WriteLine(String.Format("{0} {1}", (int)response.StatusCode, response.StatusCode));
       Console.WriteLine(response.ResponseUri);
       Console.WriteLine("Last modified: {0}", response.LastModified);
       Console.WriteLine("Server: {0}", response.Server);
       Console.WriteLine("Supports Headers: {0}", response.SupportsHeaders);
       Console.WriteLine("Headers: ");

       // do something... e.g:
       int keyCount = response.Headers.Keys.Count;
       int i = 0;
       Dictionary<string, string> hc = new Dictionary<string, string>();
       foreach (var hname in response.Headers)
       {
          var hv = response.Headers[i].ToString();
          hc.Add(hname.ToString(), hv);
          i++;
       }
       foreach (var di in hc)
       {
          Console.WriteLine("  {0} = {1}", di.Key, di.Value);
       }

   }


}
catch (Exception ex)
{
    Console.WriteLine("Exception: ");
    Console.WriteLine(ex.Message);
}   

【问题讨论】:

    标签: c# web-scraping webrequest


    【解决方案1】:

    我试过你的代码,我需要注释掉// hwr.Host = Utils.GetSimpleUrl(url);,它运行良好。如果您经常轮询,那么目标站点或介于两者之间的站点(代理、防火墙等)可能会将您的轮询识别为拒绝服务,并在一定时间内将您超时。或者,如果您位于公司防火墙后面,您可能会从内部网络设备接收到类似的信息。

    您多久运行一次此抓取工具?

    编辑添加:

    • 我使用 .net 4.52、Windows 7 x64、Visual Studio 2015 进行了尝试

    • 目标站点也可能不可靠(上下)

    • 您和目标站点之间可能存在间歇性网络问题
    • 他们可能会公开一个更可靠的集成 API

    【讨论】:

    • 我的错误。您删除的行是罪魁祸首!我的函数“GetSimpleUrl(url)”返回“magicshineuk.co.uk”,因此重定向,因为主机是在发出请求之前设置的。现在代码实际上运行良好。
    • 为了记录,我删除了行 hwr.Host = Utils.GetSimpleUrl(url);为了避免混淆。代码现在可以供其他人使用了。。
    • FTW!很高兴它现在已排序。
    猜你喜欢
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多