Skip to content

Internet

This page is about the Internet class available in PeyrSharp.Core. You can find here all of its methods.

INFO

This class is static.

Compatibility

The Internet class is part of the PeyrSharp.Core module, which is compatible with all of these frameworks and platforms:

Package/PlatformWindowsmacOSLinux + others
Core
Framework.NET 6.NET 7.NET 8
Core

Methods

IsAvailableAsync()

Definition

Checks if a connection to the Internet is available. Returns a bool value.

INFO

This method is asynchronous and awaitable.

Arguments

This method has no arguments

Usage

c#
using PeyrSharp.Core;

public static async void Main()
{
    bool isConnected = await Internet.IsAvailableAsync();
}

IsAvailableAsync(url)

Definition

Checks if a connection to the Internet is available. Returns a bool value.

INFO

This method is asynchronous and awaitable.

Arguments

TypeNameMeaning
stringurlThe page where to check if Internet is available.

Usage

c#
using PeyrSharp.Core;

public static async void Main()
{
    bool isConnected = await Internet.IsAvailableAsync("https://google.com");
}

GetStatusCodeAsync(url)

Definition

Gets the status code of a specific page by making a request to it. Returns the status code as an int value.

INFO

This method is asynchronous and awaitable.

Arguments

TypeNameMeaning
stringurlThe URL where to send the request.

Usage

c#
using PeyrSharp.Core;

public static async void Main()
{
    int status = await Internet.GetStatusCodeAsync("https://google.com");
    // status should be 200 if everything is working
}

GetStatusDescriptionAsync(url)

Definition

Gets the status description of a specific page by making a request to it. Returns the status description as a string.

INFO

This method is asynchronous and awaitable.

Arguments

TypeNameMeaning
stringurlThe URL where to send the request.

Usage

c#
using PeyrSharp.Core;

public static async void Main()
{
    string status = await Internet.GetStatusDescriptionAsync("https://google.com");
    // status should be OK if everything is working
}

GetStatusInfoAsync(url)

Definition

This method sends an HTTP GET request to a given URL and returns a Task of StatusInfo object that contains the status code, description, and type of the URL. The HTTP request method used is HttpClient.GetAsync().

Arguments

TypeNameMeaning
stringurlThe URL to get the status info from.

Returns

A Task of StatusInfo object containing the status code, description, and type of the URL.

Usage

c#
using PeyrSharp.Core;

StatusInfo statusInfo = await Internet.GetStatusInfoAsync("https://www.google.com");

GetStatusCodeType(statusCode)

Definition

Gets the type of the status code. Returns a StatusCodes value.

Arguments

TypeNameMeaning
intstatusCodeThe status code to analyze.

Usage

c#
using PeyrSharp.Core;
using PeyrSharp.Enums;

public static async void Main()
{
    int status = await Internet.GetStatusCodeAsync("https://google.com");
    switch (Internet.GetStatusCodeType(status))
    {
        case StatusCodes.Informational:
            Console.WriteLine("An information status code has been returned.");
            break;
        case StatusCodes.Success:
            Console.WriteLine("An successful status code has been returned.");
            break;
        case StatusCodes.Redirection:
            Console.WriteLine("An redirection status code has been returned.");
            break;
        case StatusCodes.ClientError:
            Console.WriteLine("An client error status code has been returned.");
            break;
        case StatusCodes.ServerError:
            Console.WriteLine("An server error status code has been returned.");
            break;
    }
}

GetUrlProtocol(url)

Definition

Gets the protocol of a specified URL. Returns a string.

Arguments

TypeNameMeaning
stringurlThe URL where to parse.

WARNING

If you provide an invalid URL, the result might be wrong or be something unexpected. An IndexOutOfRangeException can also be thrown.

Usage

c#
using PeyrSharp.Core;

string protocol = Internet.GetUrlProtocol("https://leocorporation.dev");
// protocol = https

IsUrlValid(url)

Definition

Checks if a URL is valid or not.. Returns a bool.

Arguments

TypeNameMeaning
stringurlThe URL where to check.

INFO

If you haven't specified the protocol in the URL (ex: "https://"), the "https://" string will automatically be appended to the original URL. To avoid this behavior, please specify a full URL to preserve the original protocol.

Usage

c#
using PeyrSharp.Core;

bool valid = Internet.GetUrlProtocol("a/test");
// valid = false

Definition

Downloads a file from a specified link and saves it to a specified path.

Arguments

TypeNameMeaning
stringlinkThe URL of the file to download.
stringpathThe local path where the file should be saved.

Returns

Returns true if the file was downloaded and saved successfully, otherwise returns false.

Exceptions

TypeMeaning
System.ExceptionThrows an exception if an error occurs during the download or save process.

Usage

c#
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using PeyrSharp.Core;

bool success = await Internet.DownloadFile("https://example.com/file.txt", "C:\\path\\file.txt");

if (success)
{
    Console.WriteLine("File downloaded and saved successfully.");
}
else
{
    Console.WriteLine("Failed to download or save the file.");
}

Released under the MIT License.