code4it-dev / blog-comments

https://www.code4it.dev/
1 stars 0 forks source link

blog/ping-endpoint-csharp #42

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

How to Ping an endpoint with C# - Code4IT

How to effectively ping an endpoint in C#? Don't use the HttpClient, when .NET provides a Ping class to perform all these operations.

https://www.code4it.dev/blog/ping-endpoint-csharp

gbiellem commented 1 year ago

I disagree that Ping is any better that a HEAD request. The only thing Ping is verifying for you is that something is responding to an ICMP request at the other end. At best this is telling you that the remote server is turned on ( assuming it even allows ICMP through the firewall). At least if you're issuing a HTTP request you're hitting the actual site. As for case where IsSuccessStatusCode returns false, why not simply check the HTTP status code instead. A 401, 404 or 403 or is still a response if all you want to verify you are actually hitting a web server.

bellons91 commented 1 year ago

At best this is telling you that the remote server is turned on

Yes, in fact, the very first statement of the article is:

How would you know if a remote endpoint is up and running?

But still, you have a point.

var request = new HttpRequestMessage {
    Method = HttpMethod.Head,
    RequestUri = url
};

var response = await _httpClient.SendAsync(request);

throws an HttpRequestException if the host is not known, and an HTTP Status Code otherwise (it may be any status code).