0

I am new to this site so I apologise in advance if I made any formatting errors. Let me know if further clarification is needed.

I am currently working on a program in C# which aims at downloading files from several http or ftp sources.

I want it to go through several IP addresses and then check if several methods could download a known file successful. If one of these methods could download the file successfully it should go to the next IP and do the same thing again.

So far I am using a foreach loop that runs through an array containing the IP and creating a folder named "IPxx" and a FTP and HTTP URI because I do not know in advance which IP needs a FTP or HTTP address:

string[] ipArray = new string[4];

ipArray[0]= "xxx.xxx.xxx.xx";
ipArray[1]= "xxx.xxx.xxx.xx";
ipArray[2]= "xxx.xxx.xxx.xx";
ipArray[3]= "xxx.xxx.xxx.xx";

foreach(string ip in ipArray )                                 
{
    string ipXx = "IP" + ip.Substring(ip.Length-2);             
    string ipOrdner = folder + @"\" +  ipXx;                  
    Directory.CreateDirectory(ipOrdner);                           
    string ftpAddr= "ftp://" + ip;
    string httpAddr= "http://"+ip;

    //DownloadTestMethod(httpAddr, ipFolder);
    //DownloadTestMethod2(ftpAddr, ipFolder);
    //DownloadTestMethod3(htppAddr, ipFolder);
}

So far so good, everything up to this level is working as expected. However I struggle as soon as I need to go through several Download Methods and check if I can download the file successfully and if not it should go to the next DownloadMethod and try the same.

I came up with following DownloadTestMethod:

public static void DownloadTestMethod(string httpAddr, string ipFolder)
{

    string fileName = "test_10k.bin";                    
    string downloadpath = httpAddr + "/" + fileName;

    // I want it to check if the http site is online/working   
    WebRequest request = WebRequest.Create(downloadpath);   

    request.Proxy.Credentials = CredentialCache.DefaultCredentials;

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    if (response != null)
    {

        WebClient webClient = new WebClient();
        webClient.Proxy.Credentials=CredentialCache.DefaultCredentials;                                  
        webClient.DownloadFile(downloadpath, ipFolder + @"\" + fileName);           
    }
}

It is working for a successfully downloaded file, however I seem to get "stuck" in this method if the method cannot downloaded the requested file. In this case I want it to jump to the next DownloadMethod and check again until all methods are checked.

Furthermore, if the program went through every IP and it could not download anytrhing, I want it to send an automated email. I know how to implement the EmailMethod but once again I struggle with flow control.

I am an absolute beginner and have no idea how to go from here to get my desired flow.

Ivan Leonenko
  • 2,363
  • 2
  • 27
  • 37
Timtam
  • 31
  • 1
  • 4

1 Answers1

0

You might want to look at Task Parallel Library and in particular, its WhenAll() method. Under it, you might want to look at error handling for the case that any of your methods return error / raise exception.

https://msdn.microsoft.com/en-us/library/dd270695(v=vs.110).aspx

dee zg
  • 13,793
  • 10
  • 42
  • 82