C#
Ping Cronping from C# and .NET applications.
.NET includes HttpClient out of the box — no extra packages needed.
Basic usage
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
private static readonly HttpClient _httpClient = new HttpClient
{
Timeout = TimeSpan.FromSeconds(10)
};
static async Task PingCronpingAsync(string token)
{
try
{
using var response = await _httpClient.GetAsync($"https://ping.cronping.com/{token}");
}
catch (Exception ex)
{
Console.Error.WriteLine($"Cronping ping failed: {ex.Message}");
// Don't rethrow — the job succeeded, only the notification failed
}
}
static async Task Main(string[] args)
{
// ... your job logic ...
await PingCronpingAsync("<token>");
}
}As an extension method
A clean reusable helper you can drop into any project:
using System;
using System.Net.Http;
using System.Threading.Tasks;
public static class CronpingExtensions
{
private static readonly HttpClient _client = new HttpClient
{
Timeout = TimeSpan.FromSeconds(10)
};
public static async Task PingAsync(string token)
{
try
{
using var _ = await _client.GetAsync($"https://ping.cronping.com/{token}");
}
catch (Exception ex)
{
Console.Error.WriteLine($"Cronping ping failed: {ex.Message}");
}
}
}
// Usage
await CronpingExtensions.PingAsync("<token>");With cancellation token
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
static async Task PingCronpingAsync(string token, CancellationToken cancellationToken = default)
{
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(TimeSpan.FromSeconds(10));
try
{
using var client = new HttpClient();
using var response = await client.GetAsync(
$"https://ping.cronping.com/{token}",
cts.Token
);
}
catch (Exception ex) when (ex is HttpRequestException or OperationCanceledException)
{
Console.Error.WriteLine($"Cronping ping failed: {ex.Message}");
}
}Using IHttpClientFactory (ASP.NET Core / Worker Service)
The recommended approach for long-running services:
// Program.cs
builder.Services.AddHttpClient("cronping", client =>
{
client.BaseAddress = new Uri("https://ping.cronping.com/");
client.Timeout = TimeSpan.FromSeconds(10);
});
// Worker.cs
public class MyWorker : BackgroundService
{
private readonly IHttpClientFactory _httpClientFactory;
public MyWorker(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
// ... your job logic ...
try
{
var client = _httpClientFactory.CreateClient("cronping");
using var _ = await client.GetAsync("<token>", stoppingToken);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Cronping ping failed: {ex.Message}");
}
await Task.Delay(TimeSpan.FromHours(24), stoppingToken);
}
}
}