A lightweight .NET library with utilities for common HTTP and network-related tasks:
- Creating
HttpClientinstances with a realistic User-Agent - Checking internet connectivity with multiple probing strategies
- Getting the public IP address through fallback providers
- Performing reverse DNS lookups for hostnames
HttpClientCreatorfor easyHttpClientconstructionUserAgentFetcherfor obtaining a modern Linux Firefox User-AgentNetworkUtilshelpers for connectivity checks, reverse DNS, and public IP discovery- Synchronous and asynchronous connectivity APIs
- .NET (target framework:
net10.0)
dotnet add package NuciWeb.HTTPInstall-Package NuciWeb.HTTPusing NuciWeb.HTTP;
HttpClient client = await HttpClientCreator.CreateAsync();
bool online = await NetworkUtils.HasInternetAccessAsync();
if (online)
{
string publicIp = NetworkUtils.GetPublicIpAddress();
}Creates HttpClient instances with a configured User-Agent.
HttpClient a = await HttpClientCreator.CreateAsync();
HttpClient b = await HttpClientCreator.CreateAsync(customFetcher);
HttpClient c = HttpClientCreator.Create("MyApp/1.0");Methods:
CreateAsync()CreateAsync(IUserAgentFetcher uaFetcher)Create()Create(string userAgent)
IUserAgentFetcher allows custom User-Agent provider implementations.
UserAgentFetcher implementation:
- Downloads the latest Firefox User-Agent page
- Extracts a Linux x86_64 Firefox signature
- Caches the value in-memory
- Falls back to a hardcoded modern Firefox User-Agent if extraction fails
HasInternetAccess()HasInternetAccessAsync()
Connectivity detection combines three probing strategies in parallel:
- TCP connect probes
- HTTP HEAD probes
- ICMP ping probes
The first successful strategy short-circuits the result to true.
GetPublicIpAddress()
Behavior:
- Requires internet connectivity (
HasInternetAccess) - Randomizes provider order on each call
- Returns the first non-empty response
- Throws
InvalidOperationExceptionif internet is unavailable or all providers fail
GetHostnames(IPAddress ipAddress)GetHostnames(string ipAddress)
Behavior:
- Returns a de-duplicated list containing primary hostname and aliases
- Returns an empty list when reverse DNS is unavailable for the IP
- Throws:
ArgumentNullExceptionfor nullIPAddressArgumentExceptionfor invalid IP string input
WaitForInternetAccess()WaitForInternetAccess(TimeSpan timeout)
Behavior:
- Polls connectivity once per second
- Throws
TimeoutExceptionwhen the timeout is exceeded
using NuciWeb.HTTP;
if (!NetworkUtils.HasInternetAccess())
{
NetworkUtils.WaitForInternetAccess(TimeSpan.FromSeconds(30));
}using System.Net;
using NuciWeb.HTTP;
List<string> hostnames = NetworkUtils.GetHostnames(IPAddress.Parse("1.1.1.1"));
if (hostnames.Count == 0)
{
// No reverse DNS records were available
}using System.Threading.Tasks;
using NuciWeb.HTTP;
public sealed class StaticUaFetcher : IUserAgentFetcher
{
public Task<string> GetUserAgent()
=> Task.FromResult("MyApp/1.0");
}
HttpClient client = await HttpClientCreator.CreateAsync(new StaticUaFetcher());- .NET SDK compatible with the target framework
dotnet build NuciWeb.HTTP.csprojdotnet run --project NuciWeb.HTTP.csprojdotnet testContributions are welcome.
When contributing:
- keep the project cross-platform
- preserve the existing public API unless a breaking change is intentional
- keep the changes focused and consistent with the current coding style
- update the documentation when behavior changes
- include tests for any new behavior
Licensed under the GNU General Public License v3.0 or later. See LICENSE for details.
