Table of Contents

Class HttpRequestHelper

Namespace
SunamoHttp
Assembly
SunamoHttp.dll

Helper class for HTTP request operations

public static class HttpRequestHelper
Inheritance
HttpRequestHelper
Inherited Members

Properties

ProgressBar

Gets or sets the progress bar for HTTP operations

public static IProgressBarHttp? ProgressBar { get; set; }

Property Value

IProgressBarHttp

Methods

BeforeTestingIpAddress(string)

Normalizes IP address before testing (converts ::1 to 127.0.0.1)

public static string BeforeTestingIpAddress(string ipAddress)

Parameters

ipAddress string

The IP address to normalize

Returns

string

The normalized IP address

Download(ILogger, GetResponseArgs?, string, Func<string, bool>?, string)

Downloads a file to the specified path In earlier time return ext Now return whether was downloaded

public static Task<bool> Download(ILogger logger, GetResponseArgs? args, string uri, Func<string, bool>? dontHaveAllowedExtension, string path)

Parameters

logger ILogger

The logger instance

args GetResponseArgs

The response retrieval arguments

uri string

The URI to download from

dontHaveAllowedExtension Func<string, bool>

Function to check if extension is not allowed (can be null)

path string

The destination file path

Returns

Task<bool>

True if file was downloaded, false if already exists

Download(ILogger, GetResponseArgs?, string, Func<string, bool>?, string, string, int, string?)

Downloads a file from the specified href with timeout In earlier time return ext Now return whether was downloaded

public static Task<bool> Download(ILogger logger, GetResponseArgs? args, string uri, Func<string, bool>? dontHaveAllowedExtension, string folder2, string fileName, int timeoutInMs, string? ext = null)

Parameters

logger ILogger

The logger instance

args GetResponseArgs

The response retrieval arguments

uri string

The URI to download from

dontHaveAllowedExtension Func<string, bool>

Function to check if extension is not allowed (can be null)

folder2 string

The destination folder

fileName string

The file name

timeoutInMs int

The timeout in milliseconds

ext string

The file extension (can be null)

Returns

Task<bool>

True if file was downloaded, false if already exists

Download(ILogger, GetResponseArgs?, string, Func<string, bool>?, string, string, string?)

Downloads a file from the specified href In earlier time return ext Now return whether was downloaded

public static Task<bool> Download(ILogger logger, GetResponseArgs? args, string uri, Func<string, bool>? dontHaveAllowedExtension, string folder2, string fileName, string? ext = null)

Parameters

logger ILogger

The logger instance

args GetResponseArgs

The response retrieval arguments

uri string

The URI to download from

dontHaveAllowedExtension Func<string, bool>

Function to check if extension is not allowed (can be null)

folder2 string

The destination folder

fileName string

The file name

ext string

The file extension (can be null)

Returns

Task<bool>

True if file was downloaded, false if already exists

DownloadAll(ILogger, GetResponseArgs?, List<string>, Func<string, bool>?, string, FileMoveCollisionOptionHttp, string?)

Downloads all files from the specified hrefs

public static Task DownloadAll(ILogger logger, GetResponseArgs? args, List<string> uris, Func<string, bool>? dontHaveAllowedExtension, string folder2, FileMoveCollisionOptionHttp collisionOption, string? ext = null)

Parameters

logger ILogger

The logger instance

args GetResponseArgs

The response retrieval arguments

uris List<string>

The list of URIs to download

dontHaveAllowedExtension Func<string, bool>

Function to check if extension is not allowed (can be null)

folder2 string

The destination folder

collisionOption FileMoveCollisionOptionHttp

The file move collision option

ext string

The file extension (can be null)

Returns

Task

DownloadOrRead(ILogger, string, string, DownloadOrReadArgs?)

Downloads or reads content from cache folder WARNING: Switched parameter order - A2 and A1

public static Task<string> DownloadOrRead(ILogger logger, string appDataCachePath, string uri, DownloadOrReadArgs? args = null)

Parameters

logger ILogger

The logger instance

appDataCachePath string

The cache folder path

uri string

The URI to download from

args DownloadOrReadArgs

The download or read arguments

Returns

Task<string>

The downloaded or cached content

DownloadOrReadWorker(ILogger, string, string, DownloadOrReadArgs?)

Downloads or reads a file from cache In earlier time return ext Now return whether was downloaded

public static Task<string> DownloadOrReadWorker(ILogger logger, string path, string uri, DownloadOrReadArgs? args = null)

Parameters

logger ILogger

The logger instance

path string

The local file path

uri string

The URI to download from

args DownloadOrReadArgs

The download or read arguments

Returns

Task<string>

The HTML content

ExistsPage(string)

Checks if a page exists at the specified URL

public static bool ExistsPage(string url)

Parameters

url string

The URL to check

Returns

bool

True if the page exists (HTTP 200 OK), false otherwise

GetResponseBytes(ILogger, GetResponseArgs?, string, HttpMethod, int)

Gets the response as byte array from the specified address If return empty array, SharedAlgorithms.lastError contains HttpError

public static Task<byte[]> GetResponseBytes(ILogger logger, GetResponseArgs? args, string address, HttpMethod method, int timeoutInMs = 30000)

Parameters

logger ILogger

The logger instance

args GetResponseArgs

The response retrieval arguments

address string

The URL address

method HttpMethod

The HTTP method

timeoutInMs int

The timeout in milliseconds (default 30000)

Returns

Task<byte[]>

The response bytes, or empty array on error

GetResponseStream(ILogger, GetResponseArgs?, string, HttpMethod)

Gets the response stream from the specified address

public static Stream? GetResponseStream(ILogger logger, GetResponseArgs? args, string address, HttpMethod method)

Parameters

logger ILogger

The logger instance

args GetResponseArgs

The response retrieval arguments

address string

The URL address

method HttpMethod

The HTTP method

Returns

Stream

The response stream, or null on error

GetResponseText(ILogger, GetResponseArgs?, HttpWebRequest, HttpMethod, HttpRequestData?, out HttpWebResponse?)

Gets the response text from the specified HTTP request Don't forget to Dispose the response parameter

public static string GetResponseText(ILogger logger, GetResponseArgs? args, HttpWebRequest request, HttpMethod method, HttpRequestData? httpRequestData, out HttpWebResponse? response)

Parameters

logger ILogger

The logger instance

args GetResponseArgs

The response retrieval arguments (can be null)

request HttpWebRequest

The HTTP web request

method HttpMethod

The HTTP method

httpRequestData HttpRequestData

The HTTP request configuration data (can be null)

response HttpWebResponse

The HTTP web response object (output parameter)

Returns

string

The response text

GetResponseText(ILogger, GetResponseArgs?, string, HttpMethod, HttpRequestData?)

Gets the response text from the specified address

public static string GetResponseText(ILogger logger, GetResponseArgs? args, string address, HttpMethod method, HttpRequestData? httpRequestData)

Parameters

logger ILogger

The logger instance

args GetResponseArgs

The response retrieval arguments

address string

The URL address

method HttpMethod

The HTTP method

httpRequestData HttpRequestData

The HTTP request configuration data (can be null)

Returns

string

The response text

GetResponseText(ILogger, GetResponseArgs?, string, HttpMethod, HttpRequestData?, out HttpWebResponse?)

Gets the response text from the specified address with output response

public static string GetResponseText(ILogger logger, GetResponseArgs? args, string address, HttpMethod method, HttpRequestData? httpRequestData, out HttpWebResponse? response)

Parameters

logger ILogger

The logger instance

args GetResponseArgs

The response retrieval arguments

address string

The URL address

method HttpMethod

The HTTP method

httpRequestData HttpRequestData

The HTTP request configuration data (can be null)

response HttpWebResponse

The HTTP web response object (output parameter)

Returns

string

The response text

GetResponseTextAsync(ILogger, GetResponseArgs?, string)

Gets the response text asynchronously from the specified address Is not async because of temp.Result

public static Task<string> GetResponseTextAsync(ILogger logger, GetResponseArgs? args, string address)

Parameters

logger ILogger

The logger instance

args GetResponseArgs

The response retrieval arguments

address string

The URL address

Returns

Task<string>

The response text

IsNotFound(ILogger, GetResponseArgs?, object)

Checks if the resource at the specified URI was not found

public static bool IsNotFound(ILogger logger, GetResponseArgs? args, object uri)

Parameters

logger ILogger

The logger instance

args GetResponseArgs

The response retrieval arguments

uri object

The URI to check

Returns

bool

True if resource was not found, false otherwise

SomeError(ILogger, GetResponseArgs?, object)

Checks if there was an error accessing the specified URI

public static bool SomeError(ILogger logger, GetResponseArgs? args, object uri)

Parameters

logger ILogger

The logger instance

args GetResponseArgs

The response retrieval arguments

uri object

The URI to check

Returns

bool

True if there was an error, false otherwise