feat: publish dbussmsforward refactor
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace DbusSmsForward.Helper;
|
||||
|
||||
public static class HttpHelper
|
||||
{
|
||||
public static string HttpGet(string url)
|
||||
=> HttpGetAsync(url).GetAwaiter().GetResult();
|
||||
|
||||
public static async Task<string> HttpGetAsync(string url, bool skipTlsValidation = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using HttpClient client = CreateClient(skipTlsValidation);
|
||||
using HttpResponseMessage response = await client.GetAsync(url, cancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public static string Post(string url, JsonObject obj)
|
||||
=> PostAsync(url, obj).GetAwaiter().GetResult();
|
||||
|
||||
public static async Task<string> PostAsync(string url, JsonObject obj, bool skipTlsValidation = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using HttpClient client = CreateClient(skipTlsValidation);
|
||||
using StringContent content = new(obj.ToJsonString(), Encoding.UTF8, "application/json");
|
||||
using HttpResponseMessage response = await client.PostAsync(url, content, cancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private static HttpClient CreateClient(bool skipTlsValidation)
|
||||
{
|
||||
HttpClientHandler handler = new();
|
||||
if (skipTlsValidation)
|
||||
{
|
||||
handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
|
||||
}
|
||||
|
||||
return new HttpClient(handler)
|
||||
{
|
||||
Timeout = TimeSpan.FromSeconds(30)
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user