Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

check connection c#

public static bool CheckForInternetConnection()
{
    try
    {
        using (var client = new WebClient())
            using (client.OpenRead("http://google.com/generate_204")) 
                return true; 
    }
    catch
    {
        return false;
    }
}
Comment

check connection c#


public static bool CheckForInternetConnection(int timeoutMs = 10000, string url = null)
{
    try
    {
        url ??= CultureInfo.InstalledUICulture switch
        {
            { Name: var n } when n.StartsWith("fa") => // Iran
                "http://www.aparat.com",
            { Name: var n } when n.StartsWith("zh") => // China
                "http://www.baidu.com",
            _ =>
                "http://www.gstatic.com/generate_204",
        };

        var request = (HttpWebRequest)WebRequest.Create(url);
        request.KeepAlive = false;
        request.Timeout = timeoutMs;
        using (var response = (HttpWebResponse)request.GetResponse())
            return true;
    }
    catch
    {
        return false;
    }
}

Comment

.net check connection

public static bool CheckForInternetConnection(int timeoutMs = 10000, string url = null)
{
    try
    {
        url ??= CultureInfo.InstalledUICulture switch
        {
            { Name: var n } when n.StartsWith("fa") => // Iran
                "http://www.aparat.com",
            { Name: var n } when n.StartsWith("zh") => // China
                "http://www.baidu.com",
            _ =>
                "http://www.gstatic.com/generate_204",
        };

        var request = (HttpWebRequest)WebRequest.Create(url);
        request.KeepAlive = false;
        request.Timeout = timeoutMs;
        using (var response = (HttpWebResponse)request.GetResponse())
            return true;
    }
    catch
    {
        return false;
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: recursively fing root of tree 
Csharp :: netmath 
Csharp :: mock return exception c# 
Csharp :: unity I run exe second monitor 
Csharp :: how to check if string from textbox exists in db 
Csharp :: ArgumentOutOfRangeException when sorting a DataGridView using a custom IComparer 
Csharp :: Implementing Banner Ads Unity 
Csharp :: create cursor in netezza 
Csharp :: open aspx page c# 
Csharp :: c# get app FileVersion 
Csharp :: replace bar c# 
Csharp :: Connect To MongoDB From A Different Machine 
Csharp :: c# string size in bytes 
Csharp :: cassandra Keyspaces repository .net 
Csharp :: unity organize variables in inspector 
Csharp :: parse persian date string to datetime c# 
Csharp :: c# for loop Statement 
Csharp :: pem file string reader c# 
Csharp :: how to display only date from datetime in mvc view 
Csharp :: distinct and not null c# 
Csharp :: wpf xaml group of buttons 
Csharp :: access autoload godot 
Csharp :: entity framework dynamic search 
Csharp :: how to clear stackpanel wpf 
Csharp :: c# extension method in non static class 
Csharp :: how to split a string in f# 
Csharp :: linq pick random element 
Csharp :: publish web app to linux or ubuntu 
Csharp :: listview android studio java 
Csharp :: c# loop backwards 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =