Retry failed requests in C#

For most websites, your first requests will always be successful, however, it’s inevitable that some of them will fail. For these failed requests, the API will return a 500 status code and won’t charge you for the request.

In this case, we can make our code retry to make the requests until we reach a maximum number of retries that we set:

using System;
using System.IO;
using System.Net;
using System.Web;
using System.Collections.Generic;

namespace test {
   class test{

      private static string BASE_URL = @"https://app.scrapingbee.com/api/v1/?";
      private static string API_KEY = "YOUR-API-KEY";

      public static Dictionary<string, dynamic> Get(string uri)
      {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using(Stream stream = response.GetResponseStream())
            using(StreamReader reader = new StreamReader(stream))
            {
                Dictionary<string, dynamic> OutputList = new Dictionary<string, dynamic>();
                OutputList.Add("StatusCode", response.StatusCode);
                OutputList.Add("Response", reader.ReadToEnd());
                return OutputList;
            }
      }

      public static void Main(string[] args) {

        var query = HttpUtility.ParseQueryString(string.Empty);
        query["api_key"] = API_KEY;
        query["url"] = @"https://scrapingbee.com/blog";
        string queryString = query.ToString(); // Transforming the URL queries to string

        const int MAX_RETRIES = 5; // Set the maximum number of retries we're looking to execute

        for (int i = 0; i < MAX_RETRIES; i++) {
            try {

                var output = Get(BASE_URL+queryString); // Make the request
                var StatusCode = output["StatusCode"];
                var content = output["Response"];

                if (StatusCode == HttpStatusCode.OK) { // If the response is 200/OK
                    string path = @"./ScrapingBeeBlog.html"; // Output file
                    // Create a file to write to.
                    using (StreamWriter sw = File.CreateText(path))
                    {
                        sw.Write(output);
                    }
                    Console.WriteLine("Done!");
                    break;
                } else {
                    Console.WriteLine("Failed request; Status code: " + StatusCode);
                    Console.WriteLine("Retrying...");
                }

            } catch (Exception ex) {
                Console.WriteLine("An error has occured:" + ex.Message);
                Console.WriteLine("Retrying...");
            }

        }

      }
   }
}
Go back to tutorials