RSE/Backend/Helper/HttpClientHelper.cs

70 lines
1.6 KiB
C#

using System.Diagnostics;
namespace Backend.Helper;
public static class HttpClientHelper
{
public static async Task<string> GetHtml(string url, int port)
{
using HttpClient client = new();
if (port == 80)
{
client.BaseAddress = new($"http://{url}");
}
else
{
client.BaseAddress = new($"https://{url}");
}
client.DefaultRequestHeaders.Accept.Clear();
client.Timeout = TimeSpan.FromSeconds(30);
HttpResponseMessage? response = null;
try
{
response = await client.GetAsync("/");
}
catch
{
//
}
if (response is null || !response.IsSuccessStatusCode)
{
return "";
}
return await response.Content.ReadAsStringAsync();
}
public static async Task<bool> HasRobotsTxt(string url, int port)
{
using HttpClient client = new();
if (port == 80)
{
client.BaseAddress = new($"http://{url}");
}
else
{
client.BaseAddress = new($"https://{url}");
}
client.DefaultRequestHeaders.Accept.Clear();
HttpResponseMessage? response = null;
try
{//
response = await client.SendAsync(new(HttpMethod.Head, "/robots.txt"));
}
catch
{
//
}
return response is not null && response.IsSuccessStatusCode;
}
}