user3712882
user3712882

Reputation: 61

Loop,Try -> Catch Exception until it doesn't

I have this:

return request.GetResponse() as HttpWebResponse;

And It sometimes throws exceptions when website doesn't work.. (502 error).

This particular website only goes down for a few seconds..

so.. I need to make a loop and try that above, and catch the exceptions.

I tried this:

while (true)
{
    try
    {
        return request.GetResponse() as HttpWebResponse;
        break; 
    }
    catch
    {

    }
}

But, that gives me: Unreachable code detected on break.

Upvotes: 0

Views: 385

Answers (2)

user3712882
user3712882

Reputation: 61

So.. I went and this this:

while (true)
        {
            try
            {
                return request.GetResponse() as HttpWebResponse;
            }
            catch (Exception e)
            {
                if (e is WebException && allowedRetries-- > 0)
                {
                    System.Console.WriteLine("Trying to Reconnect...");
                    Thread.Sleep((int)millisecondsDelay);
                    //millisecondsDelay *= delayMultiplyFactor;
                }
                else
                {
                    throw;
                }
            }
        }

But it gets stuck on the "Trying to Reconnect..." if I restart it.. it connects instantly.

The entire function.. if it helps:

public static HttpWebResponse Request (string url, string method, NameValueCollection data = null, CookieContainer cookies = null, bool ajax = true)
    {
        HttpWebRequest request = WebRequest.Create (url) as HttpWebRequest;

        request.Method = method;

        request.Accept = "text/javascript, text/html, application/xml, text/xml, */*";
        request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
        request.Host = "steamcommunity.com";
        request.UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11";
        request.Referer = "http://steamcommunity.com/trade/1";

        if (ajax)
        {
            request.Headers.Add ("X-Requested-With", "XMLHttpRequest");
            request.Headers.Add ("X-Prototype-Version", "1.7");
        }

        // Cookies
        request.CookieContainer = cookies ?? new CookieContainer ();

        // Request data
        if (data != null)
        {
            string dataString = String.Join ("&", Array.ConvertAll (data.AllKeys, key =>
                String.Format ("{0}={1}", HttpUtility.UrlEncode (key), HttpUtility.UrlEncode (data [key]))
            )
            );

            byte[] dataBytes = Encoding.ASCII.GetBytes (dataString);
            request.ContentLength = dataBytes.Length;

            Stream requestStream = request.GetRequestStream ();
            requestStream.Write (dataBytes, 0, dataBytes.Length);
        }

        // Get the response
        //return request.GetResponse () as HttpWebResponse;

        //EXCEPTION8712905

        double millisecondsDelay = 2000;//10
        //double delayMultiplyFactor = 2;
        int allowedRetries = 10000;//10

        while (true)
        {
            try
            {
                return request.GetResponse() as HttpWebResponse;
            }
            catch (Exception e)
            {
                if (e is WebException && allowedRetries-- > 0)
                {
                    System.Console.WriteLine("Trying to Reconnect...");
                    Thread.Sleep((int)millisecondsDelay);
                    //millisecondsDelay *= delayMultiplyFactor;
                }
                else
                {
                    throw;
                }
            }
        }
    }

Upvotes: 0

Douglas
Douglas

Reputation: 54877

As others have mentioned, the break is redundant; removing it eliminates your warning. Also, you should introduce an exponential backoff to avoid hogging your system (and flooding the server with requests) in case of recoverable failure:

double millisecondsDelay = 10;
double delayMultiplyFactor = 2;
int allowedRetries = 10;

while (true)
{
    try
    {
        return request.GetResponse() as HttpWebResponse;
    }
    catch (Exception e)
    {
        if (e is /* RecoverableException*/ && allowedRetries-- > 0)
        {
            Thread.Sleep((int)millisecondsDelay);
            millisecondsDelay *= delayMultiplyFactor;
        }
        else
        {
            throw;
        }
    }
}

Upvotes: 2

Related Questions