I have this simple function which Tries to get a pages Html, returns null if an exception is thrown
public static string TryGetPageHtml(string link, System.Net.WebProxy proxy = null)
{
System.Net.WebClient client = new System.Net.WebClient() { Encoding = Encoding.UTF8 };
if (proxy != null)
{
client.Proxy = proxy;
}
using (client)
{
try
{
return client.DownloadString(link);
}
catch (Exception ex)
{
return null;
}
}
}
i want to call this function 3 times if it returns null three times that means it faild, so i came up with the following
one way to do that is
string HTML = null;
int triesRemaining = 3;
while (HTML == null && triesRemaining--!=0)
{
HTML = TryGetPageHtml(link,getrandomproxy());
}
if(HTML == null){//do the handlig}
and another i came up with is
HTML = TryGetPageHtml(link,getrandomproxy())??TryGetPageHtml(link,getrandomproxy())??TryGetPageHtml(link,getrandomproxy());
if(HTML == null){//do the handlig}
is there a better way and are there built in things in .net that could make this more readable?