Created
March 5, 2015 07:35
-
-
Save yemrekeskin/df052c9a464cb0c9a4e2 to your computer and use it in GitHub Desktop.
Check Internet Connection with c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
if(CheckInternetConnection()) | |
Console.WriteLine("Internet var"); | |
else Console.WriteLine("Internet yok"); | |
Console.ReadLine(); | |
} | |
public static bool CheckInternetConnection() | |
{ | |
try | |
{ | |
using (var client = new WebClient()) | |
using (var stream = client.OpenRead("http://www.google.com")) | |
{ | |
return true; | |
} | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
} |
@Biyuktul Yes, use HttpClient
instead.
using var client = new HttpClient();
using var request = new HttpRequestMessage(HttpMethod.Get, "https://google.com");
using var response = client.Send(request);
I'm on mobile and just going off of memory for the class names but that should be pretty close.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
when i try this it says Webclient is deprecated
what is the alternative way to check internet conectivity in .net6 and above?