为什么我会遇到异常:在webclient上尝试过多的自动重定向?

Hai*_*ban 13 .net c# winforms

在form1的顶部,我做了:

WebClient Client;
Run Code Online (Sandbox Code Playgroud)

然后在构造函数中:

Client = new WebClient();
Client.DownloadFileCompleted += Client_DownloadFileCompleted;
Client.DownloadProgressChanged += Client_DownloadProgressChanged;
Run Code Online (Sandbox Code Playgroud)

然后我有这个方法我每分钟打电话:

private void fileDownloadRadar()
        {
            if (Client.IsBusy == true)
            {
                Client.CancelAsync();
            }
            else
            {
                Client.DownloadProgressChanged += Client_DownloadProgressChanged;
                Client.DownloadFileAsync(myUri, combinedTemp);
            }
        }
Run Code Online (Sandbox Code Playgroud)

每分钟它都会从网站上下载一张图像.这一切都工作超过24小时没有问题,直到现在在下载完成事件中抛出此异常:

private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {

            if (e.Error != null)
            {
                timer1.Stop();
                span = new TimeSpan(0, (int)numericUpDown1.Value, 0);
                label21.Text = span.ToString(@"mm\:ss");
                timer3.Start();
            }
            else if (!e.Cancelled)
            {
                label19.ForeColor = Color.Green;
                label19.Text = "????? ???????? ????? ??????";
                label19.Visible = true;
                timer3.Stop();
                if (timer1.Enabled != true)
                {
                    if (BeginDownload == true)
                    {
                        timer1.Start();
                    }
                }                
                bool fileok = Bad_File_Testing(combinedTemp);
                if (fileok == true)
                {
                    File1 = new Bitmap(combinedTemp);
                    bool compared = ComparingImages(File1);
                    if (compared == false)
                    {

                        DirectoryInfo dir1 = new DirectoryInfo(sf);
                        FileInfo[] fi = dir1.GetFiles("*.gif");
                        last_file = fi[fi.Length - 1].FullName;
                        string lastFileNumber = last_file.Substring(82, 6);
                        int lastNumber = int.Parse(lastFileNumber);
                        lastNumber++;
                        string newFileName = string.Format("radar{0:D6}.gif", lastNumber);
                        identicalFilesComparison = File_Utility.File_Comparison(combinedTemp, last_file);
                        if (identicalFilesComparison == false)
                        {
                            string newfile = Path.Combine(sf, newFileName);
                            File.Copy(combinedTemp, newfile);
                            LastFileIsEmpty();
                        }
                    }
                    if (checkBox2.Checked)
                    {
                        simdownloads.SimulateDownloadRadar();
                    }
                }
                else
                {
                    File.Delete(combinedTemp);
                }
                File1.Dispose();
            }
        }
Run Code Online (Sandbox Code Playgroud)

现在它停在if(e.Error!= null)里面就行:timer1.Stop();

然后我在错误上看到错误:这是堆栈跟踪:

at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
   at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题,以免再次发生?为什么会这样?

编辑:

我试图将fileDownloadRadar方法更改为此以每次释放客户端:

private void fileDownloadRadar()
        {
            using (WebClient client = new WebClient())
            {
                if (client.IsBusy == true)
                {
                    client.CancelAsync();
                }
                else
                {

                    client.DownloadFileAsync(myUri, combinedTemp);

                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

问题是在构造函数中我使用的是Client,这里是客户端的两个不同的Webclient变量.

我怎样才能解决这个问题和例外情况?

这是网站的websitel墨水,我每分钟都会下载图像.仍然不确定为什么我在超过24小时没有问题之后得到这个例外.现在我再次运行该程序,它正在工作,但我想知道我是否会再次获得此异常,或者有时在接下来的几个小时内.

有图像的网站我正在下载

小智 27

我遇到了与WebClient相同的问题,并在此处找到了解决方案:http: //blog.developers.ba/fixing-issue-httpclient-many-automatic-redirections-attempted/

使用HttpWebRequest并设置CookieContainer解决了这个问题,例如:

HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(linkUrl);
try
{
    webReq.CookieContainer = new CookieContainer();
    webReq.Method = "GET";
    using (WebResponse response = webReq.GetResponse())
    {
        using (Stream stream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(stream);
            res = reader.ReadToEnd();
            ...
        }
    }
}
catch (Exception ex)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)


Jim*_*hel 8

如果你得到的例外说明有太多的重定向,那是因为你试图访问的网站正在重定向到另一个站点,该站点指向另一个站点,而另一个站点则超出默认重定向限制.

因此,例如,您尝试从站点A获取图像.站点A将您重定向到站点B.站点B将您重定向到站点C等.

WebClient配置为遵循重定向到某个默认限制.由于WebClient它基于HttpWebRequest,因此它可能使用MaximumAutomaticRedirections的默认值,即50.

最有可能的是,服务器上有一个错误并且它在一个紧密的循环中重定向,或者他们已经厌倦了你每分钟对同一个文件命中服务器一次并且他们故意将你重定向到一个圆圈.

确定实际情况的唯一方法是更改​​程序,使其不会自动遵循重定向.这样,您可以检查网站返回的重定向URL并确定实际发生的情况.如果你想这样做,你需要使用HttpWebRequest而不是WebClient.

或者,您可以使用类似wget的功能,并打开详细日志记录.这将显示服务器在您发出请求时返回的内容.