Why i'm getting WebClient WebException "Too many automatic redirections were attempted" ? And how to solve it ?

  • Thread starter Thread starter Chocolade1972
  • Start date Start date
C

Chocolade1972

Guest
My program download from a website the same image every minute.

It was working fine for the last 24 hours more or less untill now it was throwing this exception.

In the top of form1 i did:


WebClient Client;



In the constructor i did:


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

Then i have this method i call it from a timer tick event every minute:


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

Every minutes it's downloading an image from a website same image each time. It was all working for more then 24 hours no problems untill now throwing this exception in the download completed event:


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();
}
}


Now it stopped inside the if(e.Error != null) On the line: timer1.Stop();

Then i see on the Error the error: This is the stack trace:


at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at
System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
at
System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)



I tried to change the fileDownloadRadar method to this to release the client every time:


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

client.DownloadFileAsync(myUri, combinedTemp);

}
}
}

The problem is that in the constructor i'm using Client and here it's client two different Webclient variables.

How can i solve this and the exception ?




Continue reading...
 
Back
Top