41 lines
1.2 KiB
C#
41 lines
1.2 KiB
C#
|
// The URL of the API or website you want to make a request to
|
||
|
using System;
|
||
|
using System.Net.Http;
|
||
|
using System.Text;
|
||
|
using System.Threading.Tasks;
|
||
|
|
||
|
static class HTTP {
|
||
|
|
||
|
private static readonly HttpClient client = new HttpClient(){
|
||
|
Timeout = TimeSpan.FromSeconds(10) // Set a timeout to avoid hanging
|
||
|
};
|
||
|
|
||
|
public static async Task<string> get(string url){
|
||
|
try
|
||
|
{
|
||
|
Console.WriteLine("SUCCESS\n " + url);
|
||
|
|
||
|
HttpResponseMessage response = client.GET(url);
|
||
|
|
||
|
Console.WriteLine("SUCCESS3\n " + url);
|
||
|
|
||
|
if (response.IsSuccessStatusCode)
|
||
|
{
|
||
|
string responseData = await response.Content.ReadAsStringAsync();
|
||
|
|
||
|
return responseData;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Console.WriteLine("Error: " + response.StatusCode);
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Console.WriteLine("Exception occurred: " + ex.Message);
|
||
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
}
|