Search code examples
c#webclient

As a C# noob, how do I retry WebClient.DownloadString funtion


I am AN ABSOLUTE C# NOOB, so this is my very first project. I am write a C% command line program that I input a URL it downloads images from a gallery website I frequent. 99% of the the code I have written works. My only issue is sometimes my code, when trying to get the webpage source code, sometimes the download fails. 99% of what I am doing works. This 1% sometimes makes my program crash. I am trying to find a way to make WebClient.DownloadString will retry to download the page source if there is an error. With all that said above, 99% of the code is written, and working, so I will not rewrite it from the ground up in a different version of .net. (A suggestion I saw in a discord channel dedicated to C# coding that was not helpful in the least)

This is the code I have for downloading the page source:

static string getPageSource(string url)
{
  string pageSource = "";
  WebClient client = new WebClient();

  pageSource = client.DownloadString(url);
  if(pageSource.Length == 0)
    {
    pageSource = client.DownloadString(url);
    }
  return pageSource;
}

The code itself functions as is. Just whenever it fails, the app crashes.


Solution

  • The first step is to always determine how the application is failing. To do that, you run it in a debugger. In your case, it's probably throwing an exception in the DownloadString method, so your second attempt is never being called.

    It's just a guess, based on the limited information you've provided. Based on your post, you're apparently a newb to programming in general yes? You're obviously not thinking like a programmer yet, but we should expect that from newbs.

    I recommend you follow the links in my comment to your original post and read those documents thoroughly. They will help you understand how to approach asking questions here. As for thinking like a programmer, always start with a careful read of the documentation for the API's you are using:

    https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadstring?view=net-8.0

    Take note of the exception list:

    Exceptions ArgumentNullException The address parameter is null.

    WebException The URI formed by combining BaseAddress and address is invalid.

    -or-

    An error occurred while downloading the resource.

    NotSupportedException The method has been called simultaneously on multiple threads.

    In your case, it's probably throwing WebException due to a server timeout or because it's denying you access. You may be exceeding a download limit.

    Read up on how to catch exceptions:

    https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/exception-handling-statements

    Place your code in a try/catch block:

        string getPageSource(string url)
        {
            var pageSource = string.Empty;
    
            try
            {
                using var client = new WebClient();
                pageSource = client.DownloadString(url);
            }
            catch(WebException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch(Exception ex)
            {
                Console.WriteLine($"Unexpeccted exception {ex.Message}");
            }
    
            return pageSource;
        }
    

    Run that and tell us which exception is caught, then I'll show you how to properly do a limited number of retries.


    NOTE: WebClient is obsolete and should not be used. If your instructor tells you to use it, then use it and smother it in comments about how good coders would not use and why.