try server connection by evaluating server reply
<p>I am makeing a program in C# which needs to have a connection with a server.
I'm building it with redundancy in order to eliminate server faults.
Due to this, I'm having a textfile with server addresses which looks like this:</p>

<pre><code>2011-09-21 18:01
http://server1.server.com
http://server2.server.com
http://server3.server.com
</code></pre>

<p>The date is there to make sure the program has a fresh list of servers.
The first thing it does when it starts, is to download this list and compare the date.
If the downloaded file has a newer date, it replaces the older files.</p>

<p>What I wan't to happen now, is that I want the program to read the file and make a list of possible servers (this is already done), and then iterate through the list to find a server that is giving the reply the server is expecting.</p>

<p>Once the reply is gotten, it should stop checking and set the variable "server" to the server that just responded correctly.</p>

<p>I have been trying with the following procedures, without any luck:</p>

<pre><code>int srvs = servers.Count();
int i = 0;
string response = string.Empty;
while (i &lt; srvs)
{
    var client = new WebClient();
    try
    {
        response = client.DownloadString(servers[i] + "testcon.php");
    }
    catch (WebException wex)
    {
    }
    finally
    {
        if (response == "OK")
        {
            server = servers[i];
        }
    }
    if (servers.Contains(server))
        break;
    i++;
}
</code></pre>

<p>This didn't work, it didn't stop the loop until it was at the end of the list.
Next thing I tried, was this:</p>

<pre><code>var client = new WebClient();
foreach (string s in servers)
{
    try
    {
        response = client.DownloadString(s + "testcon.php");
    }
    catch (WebException wex)
    {
    }
    finally
    {
    if (response == "OK")
        server = s;
    }
        break;
    }
</code></pre>

<p>How should I do to make it stop and set the server to the first server that is responding as expected (currently with "OK").</p>
