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

前端之家收集整理的这篇文章主要介绍了c# – 为什么我会收到异常:在webclient上尝试过多的自动重定向?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在form1的顶部我做到了:
WebClient Client;

然后在构造函数中:

Client = new WebClient();
Client.DownloadFileCompleted += Client_DownloadFileCompleted;
Client.DownloadProgressChanged += Client_DownloadProgressChanged;

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

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

每一分钟,它每次从网站下载图像.
这是所有工作超过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();
            }
        }

现在它停在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)

我如何解决这个问题呢,不会再发生了?为什么会发生?

编辑:

我尝试将fileDownloadRadar方法更改为每次发布客户端:

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

                    client.DownloadFileAsync(myUri,combinedTemp);

                }
            }
        }

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

我如何解决这个和例外?

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

The site with image i’m downloading

解决方法

我和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)
{
    ...
}

猜你在找的C#相关文章