.Net WebException to check if the local machine has Internet access

I am working on a Winforms application and use C # as a development language. In one place, I need to send a request to a URL and get feedback.

I use my code inside a catch try block and I will catch a WebException. I noticed that WebException is due to two reasons.

(1) when the URL entered is incorrect (URL that does not exist)

(2) If there is no internet connection

So well, if I catch a WebException in a situation where the URL is correct, to check that there is no Internet connection on the local computer. (I need to check if an internet connection exists or not in order to continue the next step)

Can anyone confirm that my proposal is correct or, if not, why? Thanks inadvance

+4
source share
2 answers

You cannot check your internet connection while trying to connect to a url. You cannot tell between a bad connection or an unavailable server.

Wininet has win32 API - see check if internet connection is available with C #

+2
source

I used this function (on vb.net) to find out if the internet is available:

Public Function HasInternet() As Boolean Dim response As Boolean = False Try response = My.Computer.Network.Ping("www.google.com") If Not response Then 'Try another one response = My.Computer.Network.Ping("www.yahoo.com") End If Catch ex As Exception 'Catch the error or leave it blank for returning False End Try Return response End Function 

The only problem is that if google.com and yahoo.com stop responding to ping requests, they are not reporting an internet connection.

0
source

Source: https://habr.com/ru/post/1393156/


All Articles