RSE/Backend/Helper/HttpClientHelper.cs
2024-11-24 12:40:51 +01:00

67 lines
1.5 KiB
C#

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();
HttpResponseMessage? response = null;
try
{
response = await client.GetAsync("/");
}
catch (Exception e)
{
Console.WriteLine(e);
}
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.GetAsync("/robots.txt");
}
catch (Exception e)
{
Console.WriteLine(e);
}
return response is not null && response.IsSuccessStatusCode;
}
}