Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When you browse on the Web for any site, you get status codes either for good or for bad:
Examples
200 GOOD - everything went alright
404 BAD - Site not found

What will I get if my Internet connection drops?
Is there an HTTP code for that kind of problem?

If there is no such code, then you can probably still help me ;) I need to catch the exception on .NET with C#. Just please, let's not go for a workaround, I really need to know if there is no Internet connection so I can handle the error different from a 404.


C#
try
{
    //HttpWebRequest req = new ...
}
catch (WebException ex)
{
    //  How do I get no internet connection ?
}


Thank you very much!!
Posted
Updated 20-Oct-13 12:11pm
v2

Why not to handle as Exception
C#
try
{
    //HttpWebRequest req = new ...
}
catch (WebException ex)
{
    
}
catch (Exception ex)
{
   
}
 
Share this answer
 
v2
Comments
Homero Rivera 20-Oct-13 18:58pm    
The problem is that I am building a webcrawler and have all URIs to crawl in a database. If I get a 404 error, I want to declare that URI as invalid in the database. If I have no Internet connection I would simply pause my threads or make them check periodically if connection is back to resume without ruling out the URI.
Think this will work?
Dave Kreskowiak 20-Oct-13 19:35pm    
The problem is that it depends on when in the connect/request/response/disconnect process your connect drops. Most of the time, you'll get some kind of 400 or 500 response, but you can also get non-HTTP errors if the connection disappears before the host name is resolved.
Homero Rivera 22-Oct-13 10:35am    
Thank you, by next weekend I will be reviewing all your replies. Im sorry but I got a lot of work weekdays
You can also use

System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()

Returns True if connection available else it will return false

Here's a nice article you can refer

Detect Internet Network Availability[^]
 
Share this answer
 
Comments
Dave Kreskowiak 20-Oct-13 21:38pm    
This has nothing to do with the possible HTTP errors that come back if the connection fails in the middle of the request.
Homero Rivera 22-Oct-13 10:35am    
Thank you, by next weekend I will be reviewing all your replies. Im sorry but I got a lot of work weekdays
Homero Rivera 27-Oct-13 13:03pm    
This seems like the best answer.
Kudos!
Ranjan.D 27-Oct-13 13:20pm    
Thank You.. Glad you got the solution :)
The Error "The remote name could not be resolved: " is indicating that network unavailability.

So try this code by disabling network.


C#
try
{
      WebRequest rc = HttpWebRequest.Create("http://www.google.com");
      rc.GetResponse();
      }
      catch (WebException ex)
      {
//The remote name could not be resolved: 'www.google.com' error is indicating the Network unavailability
      }
 
Share this answer
 
v3
Comments
Homero Rivera 22-Oct-13 10:35am    
Thank you, by next weekend I will be reviewing all your replies. Im sorry but I got a lot of work weekdays
Homero Rivera 27-Oct-13 12:51pm    
TRUE, that is what you get when no connection is available.
Accepted as answer, but I'm yet to find how to properly detect a lack of connection since the error message is ambiguous. See, that is the same message you get when the page doesn't exist (domain name does not exist). Try browsing with C# http://www.themostridiculousandunthinkabledomainname.com and you'll get the same error message as when internet connection drops.
Quote:
What will I get if my Internet connection drops?
Is there an HTTP code for that kind of problem?


HTTP Response Codes are just that, response codes. If you have a loss of connection you don't get a response code, you get no response at all. Response codes (like 200, 404, 500, etc) come directly from the server. If you can't connect to the server then you won't get any code.

Depending on how you write the code, you could get a socket error or it would hang. This is especially problematic if you lose connection in the middle of a request. The best solution is a combination of Solution 1 and Solution 2.
 
Share this answer
 
Comments
Homero Rivera 22-Oct-13 10:35am    
Thank you, by next weekend I will be reviewing all your replies. Im sorry but I got a lot of work weekdays
Homero Rivera 27-Oct-13 12:57pm    
TRUE, yet we do get an exception which is ambiguous, "The remote name could not be resolved".
You get that both when there is no connection and when the domain name does not exist.

Thank you for your answer!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900