NetworkConnection

Introduction

Do not forget to add this line of code in your "using" region:

C#

using LeoCorpLibrary;

VB

Imports LeoCorpLibrary

Functions

There are various methods available in NetworkConnection to help achieve what you wanna do.

They are in:

LeoCorpLibrary.NetworkConnection.IsAvailable()

a. IsAvailable

This method is available in version 1.1 and higher.

Compatibility
FrameworksLeoCorpLibraryLeoCorpLibrary.Core
.NET 6
.NET 5
.NET Core 3.1
.NET Framework 4.5

This method tries to connect to https://bing.com by default. If it receives a positive status code (like 200), it will return true.

You can specify a custom website if you want to:

ValueArgumentDescription
stringurlThe URL of the website where the connection is going to be tested

Note

Specifying a custom website is available starting with version 4.1+.

Here's an example of usage:

C#

if (NetworkConnection.IsAvailable()) 
{
    Console.WriteLine("Connected");
}
else
{
    Console.WriteLine("Not connected");
}

// Or
bool isAvailable = NetworkConnection.IsAvailable("https://leocorporation.dev");

VB

If (NetworkConnection.IsAvailable()) Then
    Console.WriteLine("Connected")
Else
    Console.WriteLine("Not connected")
End If

' Or
// Or
Dim isAvailable As Boolean = NetworkConnection.IsAvailable("https://leocorporation.dev")

b. IsAvailableTestSite

This method is available in version 1.1 and higher.

Obsolete

This method is obsolete, please use IsAvailable() instead.

Compatibility
FrameworksLeoCorpLibraryLeoCorpLibrary.Core
.NET 6
.NET 5
.NET Core 3.1
.NET Framework 4.5

This method is going to try to connect to a specific page that you specified. If it manages to connect to that page, it means there is an Internet connection.

You must specify a website like this:

Example:

IsAvailableTestSite("https://example.com")

Here's an example of usage:

C#

if (NetworkConnection.IsAvailableTestSite("https://example.com")) { // If true
    Console.WriteLine("Connected");
}else{
    Console.WriteLine("Not connected");
}

VB

If (NetworkConnection.IsAvailableTestSite("https://example.com")) Then ' If true
    Console.WriteLine("Connected")
Else
    Console.WriteLine("Not connected")
End If

Go to top

c. IsAvailableAsync

This method is available in version 2.1 and higher.

Compatibility
FrameworksLeoCorpLibraryLeoCorpLibrary.Core
.NET 6
.NET 5
.NET Core 3.1
.NET Framework 4.5

This method tries to connect to https://bing.com by default. If it receives a positive status code (like 200), it will return true.

You can specify a custom website if you want to:

ValueArgumentDescription
stringurlThe URL of the website where the connection is going to be tested

Note

Specifying a custom website is available starting with version 4.1+.

It's in:

LeoCorpLibrary.NetworkConnection.IsAvailableAsync()

Here's an example of usage:

C#

async void TestConnection()
{
    bool connectionAvailable = await NetworkConnection.IsAvailableAsync();

    // Or
    bool connectionAvailable2 = await NetworkConnection.IsAvailableAsync("https://leocorporation.dev");
}

VB

Private Async Sub TestConnection()
    Dim connectionAvailable As Boolean = Await NetworkConnection.IsAvailableAsync()

    ' Or
    Dim connectionAvailable2 As Boolean = Await NetworkConnection.IsAvailableAsync("https://leocorporation.dev")
End Sub

Go to top

d. IsAvailableTestSiteAsync

This method is available in version 2.1 and higher.

Obsolete

This method is obsolete, please use IsAvailableAsync() instead.

Compatibility
FrameworksLeoCorpLibraryLeoCorpLibrary.Core
.NET 6
.NET 5
.NET Core 3.1
.NET Framework 4.5

This method verifies if an Internet connection is available by sending a request on a specified website. Returns a bool value.

It's in:

LeoCorpLibrary.NetworkConnection.IsAvailableTestSiteAsync()

It has one argument:

ValueArgumentDescription
stringsiteURL of the website where the connection is tested

Here's an example of usage:

C#

async void TestConnection()
{
    bool connectionAvailable = await NetworkConnection.IsAvailableTestSiteAsync("https://leocorporation.dev");
}

VB

Private Async Sub TestConnection()
    Dim connectionAvailable As Boolean = Await NetworkConnection.IsAvailableTestSiteAsync("https://leocorporation.dev")
End Sub

Go to top

e. GetWebPageStatusCode

This method is available in version 4.1 and higher.

Compatibility
FrameworksLeoCorpLibraryLeoCorpLibrary.Core
.NET 6
.NET 5
.NET Core 3.1
.NET Framework 4.5

Obsolete

This method is obsolete, please use GetWebPageStatusCodeAsync() instead.

The GetWebPageStatusCode() method allows you to get the status code of a specified website. It returns an int value.

Learn more about status codesopen in new window.

It's in:

LeoCorpLibrary.NetworkConnection.GetWebPageStatusCode()

It has one argument:

ValueArgumentDescription
stringurlThe URL of the website

Here's an example of usage:

C#

if (NetworkConnection.GetWebPageStatusCode("https://leocorporation.dev") == 200) 
{
    Console.WriteLine("The website responded: OK");
}

VB

If NetworkConnection.GetWebPageStatusCode("https://leocorporation.dev") = 200 Then
    Console.WriteLine("The website responded: OK")
End if

Go to top

f. GetWebPageStatusCodeAsync

This method is available in version 4.5 and higher.

Compatibility
FrameworksLeoCorpLibraryLeoCorpLibrary.Core
.NET 6
.NET 5
.NET Core 3.1
.NET Framework 4.5

The GetWebPageStatusCodeAsync() method allows you to get the status code of a specified website asynchronously. It returns an int value.

Learn more about status codesopen in new window.

It's in:

LeoCorpLibrary.NetworkConnection.GetWebPageStatusCodeAsync()

It has one argument:

ValueArgumentDescription
stringurlThe URL of the website

Here's an example of usage:

C#

internal async Task Main() 
{
    if (await NetworkConnection.GetWebPageStatusCodeAsync("https://leocorporation.dev") == 200) 
    {
        Console.WriteLine("The website responded: OK");
    }
}

VB

Friend Async Function Main()
    If Await NetworkConnection.GetWebPageStatusCodeAsync("https://leocorporation.dev") = 200 Then
        Console.WriteLine("The website responded: OK")
    End If
End Function

Go to top

g. GetWebPageStatusDescription

This method is available in version 4.1 and higher.

Compatibility
FrameworksLeoCorpLibraryLeoCorpLibrary.Core
.NET 6
.NET 5
.NET Core 3.1
.NET Framework 4.5

Obsolete

This method is obsolete, please use GetWebPageStatusDescriptionAsync() instead.

The GetWebPageStatusDescription() method allows you to get the status description of a specified website. For instance, if the status code of the website is 418, the status description associated with this status code will be "I'm a teapot". It returns a string value.

Learn more about status codesopen in new window.

It's in:

LeoCorpLibrary.NetworkConnection.GetWebPageStatusDescription()

It has one argument:

ValueArgumentDescription
stringurlThe URL of the website

Here's an example of usage:

C#

int status = NetworkConnection.GetWebPageStatusCode("https://leocorporation.dev");
string description = NetworkConnection.GetWebPageStatusDescription("https://leocorporation.dev");

Console.WriteLine(status.ToString() + " - " + description);
// Expected output:
// 200 - OK

VB

Dim status As Integer = NetworkConnection.GetWebPageStatusCode("https://leocorporation.dev")
Dim description As String = NetworkConnection.GetWebPageStatusDescription("https://leocorporation.dev")

Console.WriteLine(status.ToString() & " - " & description)
' Expected output:
' 200 - OK

Go to top

h. GetWebPageStatusDescriptionAsync

This method is available in version 4.5 and higher.

Compatibility
FrameworksLeoCorpLibraryLeoCorpLibrary.Core
.NET 6
.NET 5
.NET Core 3.1
.NET Framework 4.5

The GetWebPageStatusDescriptionAsync() method allows you to get the status description of a specified website asynchronously. For instance, if the status code of the website is 418, the status description associated with this status code will be "I'm a teapot". It returns a string value.

Learn more about status codesopen in new window.

It's in:

LeoCorpLibrary.NetworkConnection.GetWebPageStatusDescriptionAsync()

It has one argument:

ValueArgumentDescription
stringurlThe URL of the website

Here's an example of usage:

C#

internal async Task Main()
{
    int status = await NetworkConnection.GetWebPageStatusCodeAsync("https://leocorporation.dev");
    string description = await NetworkConnection.GetWebPageStatusDescriptionAsync("https://leocorporation.dev");

    Console.WriteLine(status.ToString() + " - " + description);
    // Expected output:
    // 200 - OK
}

VB

Friend Async Function Main()
    Dim status As Integer = Await NetworkConnection.GetWebPageStatusCodeAsync("https://leocorporation.dev")
    Dim description As String = Await NetworkConnection.GetWebPageStatusDescriptionAsync("https://leocorporation.dev")
    
    Console.WriteLine(status.ToString() & " - " & description)

    ' Expected output:
    ' 200 - OK
End Function

Go to top

i. GetStatusCodeType

This method is available in version 4.1 and higher.

Compatibility
FrameworksLeoCorpLibraryLeoCorpLibrary.Core
.NET 6
.NET 5
.NET Core 3.1
.NET Framework 4.5

The GetStatusCodeType() method allows you to get the "type" of a status code. For instance, if the status code is between 100 and 199, the method will return StatusCodeType.Informational. It returns a StatusCodeType enum value.

Learn more about status codesopen in new window.

It's in:

LeoCorpLibrary.NetworkConnection.GetStatusCodeType()

It has one argument:

ValueArgumentDescription
stringurlThe URL of the website

Here's an example of usage:

C#

if (NetworkConnection.GetStatusCodeType("https://leocorporation.dev") == StatusCodeType.Success)
{
    Console.WriteLine("The request was successful!")
}
else if (NetworkConnection.GetStatusCodeType("https://leocorporation.dev" == StatusCodeType.ClientError))
{
    Console.WriteLine("The request was not successful!")
}

VB

If NetworkConnection.GetStatusCodeType("https://leocorporation.dev") = StatusCodeType.Success Then
    Console.WriteLine("The request was successful!")
ElseIf NetworkConnection.GetStatusCodeType("https://leocorporation.dev" = StatusCodeType.ClientError) Then
    Console.WriteLine("The request was not successful!")
End If

Go to top

j. DownloadFileAsync

This method is available in version 4.4 and higher.

Compatibility
FrameworksLeoCorpLibraryLeoCorpLibrary.Core
.NET 6
.NET 5
.NET Core 3.1
.NET Framework 4.5

The DownloadFileAsync() method allows you to download any files from the Internet using the HttpClient class instead of the WebClient class. It doesn't return any value (Task).

It's in:

LeoCorpLibrary.NetworkConnection.DownloadFileAsync()

It has one argument:

ValueArgumentDescription
UriuriThe URI of the file to download
stringfilePathThe path where to store the file once downloaded

Here's an example of usage:

C#

async Task Main()
{
    await NetworkConnection.DownloadFileAsync(new Uri("https://example.com/file.pdf"), @"C:\myFile.pdf");
}

VB

Private Async Function Main()
    Await NetworkConnection.DownloadFileAsync(New Uri("https://example.com/file.pdf"), "C:\myFile.pdf")
End Function

Go to top