1234567891011121314151617181920212223242526272829303132 |
- using System.Net;
- using static CompServ.CompServConst;
- namespace CompServ.ClientLibrary;
- public abstract class CompServClient(string server, string apiCheckAlive, string aliveMessage)
- {
- protected readonly Uri ServerUri = new Uri(server);
- public async Task<bool> CheckAliveAsync(int timeoutInSecond = 1)
- {
- try
- {
- var httpRequest = new HttpRequestMessage(HttpMethod.Get, apiCheckAlive)
- {
- Version = HttpVersion.Version20,
- VersionPolicy = HttpVersionPolicy.RequestVersionExact,
- };
- using var client = new HttpClient { BaseAddress = ServerUri };
- client.Timeout = TimeSpan.FromSeconds(timeoutInSecond);
- var httpResponse = await client.SendAsync(httpRequest);
- httpResponse.EnsureSuccessStatusCode();
- var rm = await httpResponse.Content.ReadAsStringAsync();
- return rm == aliveMessage;
- }
- catch
- {
- return false;
- }
- }
- }
|