I am trying to download files through webclient and the code is like the one given below. The problem is that if I get continuous 404 response alot of times, my server reaches to 100% and viewing the event log tells that stack overflow happened. Here the “count” variable is to avoid 0 byte files and count404 is for 404 responses.
int count = 0; int count404 = 0;
public Stream DownloadFileThroughWebClient(string strFilePath)
{
try
{
if (count >= 120 || count404 >= 30)
{
count = 0;
count404 = 0;
return null;
}
System.Threading.Thread.Sleep(1000);
System.Net.WebClient wc = new System.Net.WebClient();
var v = wc.DownloadData(strFilePath);
Stream FileToSave = new MemoryStream(v);
byte[] bytes = new byte[FileToSave.Length];
int numBytesToRead = (int)FileToSave.Length;
if (numBytesToRead > 0)
{
count = 0;
count404 = 0;
return FileToSave;
}
else
{
count++;
count404 = 0;
return DownloadFileThroughWebClient(strFilePath);
}
}
catch (Exception ex)
{
count++;
count404++;
return DownloadFileThroughWebClient(strFilePath);
}
}
Thanks in advance.
Try this (without using recursion and counts the 404s correctly):