Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6232309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:05:41+00:00 2026-05-24T10:05:41+00:00

So my application is exchanging request/responses with a server (no problems), until the internet

  • 0

So my application is exchanging request/responses with a server (no problems), until the internet connection dies for a couple of seconds, then comes back. Then a code like this:

response = (HttpWebResponse)request.GetResponse();

will throw an exception, with a status like ReceiveFailure, ConnectFailure, KeepAliveFailure etc.

Now, it’s quite important that if the internet connection comes back, I am able to continue communicating with the server, otherwise I’d have to start again from the beginning and that will take a long time.

How would you go about resuming this communication when the internet is back?

At the moment, I keep on checking for a possibility to communicate with the server, until it is possible (at least theoretically). My code attempt looks like this:

try
{
    response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
    // We have a problem receiving stuff from the server. 
    // We'll keep on trying for a while
    if (ex.Status == WebExceptionStatus.ReceiveFailure || 
        ex.Status == WebExceptionStatus.ConnectFailure || 
        ex.Status == WebExceptionStatus.KeepAliveFailure)
    {
        bool stillNoInternet = true;

        // keep trying to talk to the server
        while (stillNoInternet)
        {
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                stillNoInternet = false;
            }
            catch
            {
                stillNoInternet = true;
            }
        }
    }
}

However, the problem is that the second try-catch statement keeps throwing an exception even when the internet is back.

What am I doing wrong? Is there another way to go about fixing this?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:05:43+00:00Added an answer on May 24, 2026 at 10:05 am

    You should recreate the request each time, and you should execute the retries in a loop with a wait between each retry. The wait time should progressively increase with each failure.

    E.g.

    ExecuteWithRetry (delegate {
        // retry the whole connection attempt each time
        HttpWebRequest request = ...;
        response = request.GetResponse();
        ...
    });
    
    private void ExecuteWithRetry (Action action) {
        // Use a maximum count, we don't want to loop forever
        // Alternativly, you could use a time based limit (eg, try for up to 30 minutes)
        const int maxRetries = 5;
    
        bool done = false;
        int attempts = 0;
    
        while (!done) {
            attempts++;
            try {
                action ();
                done = true;
            } catch (WebException ex) {
                if (!IsRetryable (ex)) {
                    throw;
                }
    
                if (attempts >= maxRetries) {
                    throw;
                }
    
                // Back-off and retry a bit later, don't just repeatedly hammer the connection
                Thread.Sleep (SleepTime (attempts));
            }
        }
    }
    
    private int SleepTime (int retryCount) {
        // I just made these times up, chose correct values depending on your needs.
        // Progressivly increase the wait time as the number of attempts increase.
        switch (retryCount) {
            case 0: return 0;
            case 1: return 1000;
            case 2: return 5000;
            case 3: return 10000;
            default: return 30000;
        }
    }
    
    private bool IsRetryable (WebException ex) {
        return
            ex.Status == WebExceptionStatus.ReceiveFailure ||
            ex.Status == WebExceptionStatus.ConnectFailure ||
            ex.Status == WebExceptionStatus.KeepAliveFailure;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble exchanging my Oauth request token for an Access Token. My
Hi I am implementing P2P chatting application where server will mediator for exchangeing IP
Application requests KML data through AJAX from server. This data is stored in javascript
My application was running fine until i added the following code to add an
I have developed a chat web application which uses a SqlServer database for exchanging
Application db connection in web service. i haveto connect the db using that web
Application connected to MS SQL Server will create views where a single row result
What is the best way of exchanging data between a C# desktop application and
Application able to record error in OnError, but we are not able to do
Application frameworks such as DotNetNuke, Eclipse, Websphere and so forth are available today which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.