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
Methods
BeforeTestingIpAddress(string)
Normalizes IP address before testing (converts ::1 to 127.0.0.1)
public static string BeforeTestingIpAddress(string ipAddress)
Parameters
ipAddressstringThe 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
loggerILoggerThe logger instance
argsGetResponseArgsThe response retrieval arguments
uristringThe URI to download from
dontHaveAllowedExtensionFunc<string, bool>Function to check if extension is not allowed (can be null)
pathstringThe destination file path
Returns
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
loggerILoggerThe logger instance
argsGetResponseArgsThe response retrieval arguments
uristringThe URI to download from
dontHaveAllowedExtensionFunc<string, bool>Function to check if extension is not allowed (can be null)
folder2stringThe destination folder
fileNamestringThe file name
timeoutInMsintThe timeout in milliseconds
extstringThe file extension (can be null)
Returns
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
loggerILoggerThe logger instance
argsGetResponseArgsThe response retrieval arguments
uristringThe URI to download from
dontHaveAllowedExtensionFunc<string, bool>Function to check if extension is not allowed (can be null)
folder2stringThe destination folder
fileNamestringThe file name
extstringThe file extension (can be null)
Returns
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
loggerILoggerThe logger instance
argsGetResponseArgsThe response retrieval arguments
urisList<string>The list of URIs to download
dontHaveAllowedExtensionFunc<string, bool>Function to check if extension is not allowed (can be null)
folder2stringThe destination folder
collisionOptionFileMoveCollisionOptionHttpThe file move collision option
extstringThe file extension (can be null)
Returns
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
loggerILoggerThe logger instance
appDataCachePathstringThe cache folder path
uristringThe URI to download from
argsDownloadOrReadArgsThe download or read arguments
Returns
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
loggerILoggerThe logger instance
pathstringThe local file path
uristringThe URI to download from
argsDownloadOrReadArgsThe download or read arguments
Returns
ExistsPage(string)
Checks if a page exists at the specified URL
public static bool ExistsPage(string url)
Parameters
urlstringThe 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
loggerILoggerThe logger instance
argsGetResponseArgsThe response retrieval arguments
addressstringThe URL address
methodHttpMethodThe HTTP method
timeoutInMsintThe timeout in milliseconds (default 30000)
Returns
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
loggerILoggerThe logger instance
argsGetResponseArgsThe response retrieval arguments
addressstringThe URL address
methodHttpMethodThe 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
loggerILoggerThe logger instance
argsGetResponseArgsThe response retrieval arguments (can be null)
requestHttpWebRequestThe HTTP web request
methodHttpMethodThe HTTP method
httpRequestDataHttpRequestDataThe HTTP request configuration data (can be null)
responseHttpWebResponseThe 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
loggerILoggerThe logger instance
argsGetResponseArgsThe response retrieval arguments
addressstringThe URL address
methodHttpMethodThe HTTP method
httpRequestDataHttpRequestDataThe 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
loggerILoggerThe logger instance
argsGetResponseArgsThe response retrieval arguments
addressstringThe URL address
methodHttpMethodThe HTTP method
httpRequestDataHttpRequestDataThe HTTP request configuration data (can be null)
responseHttpWebResponseThe 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
loggerILoggerThe logger instance
argsGetResponseArgsThe response retrieval arguments
addressstringThe URL address
Returns
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
loggerILoggerThe logger instance
argsGetResponseArgsThe response retrieval arguments
uriobjectThe 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
loggerILoggerThe logger instance
argsGetResponseArgsThe response retrieval arguments
uriobjectThe URI to check
Returns
- bool
True if there was an error, false otherwise