在WebClient中尝试了太多自动重定向

时间:2017-10-09 15:05:09

标签: c#

我需要在WebClient中允许重定向,因为我有这个网址:

http://int.soccerway.com/national/algeria/ligue-2/c207/

当我在浏览器中添加此内容时,我将重定向到此:

http://int.soccerway.com/national/algeria/ligue-2/20172018/regular-season/r43168/

当我执行请求时,我执行此方法:

    public static string GetData(string url)
    {
        HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(url);

        try
        {
            webReq.CookieContainer = new CookieContainer();
            webReq.Method = "GET";
            webReq.AllowAutoRedirect = true;
            webReq.MaximumAutomaticRedirections = 1;

            using (WebResponse response = webReq.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(stream);
                    return reader.ReadToEnd();
                }
            }
      ...
   }

但这会引发异常:

  

尝试了太多的自动重定向

其他线程建议添加cookie容器,我这样做但错误发生再次发生。我怎么解决这个问题?

0 个答案:

没有答案