internal static class ServiceRequest
{
public static string APIurl { get; set; } = "https://localhost:5001"; //
public static async Task<(string result,string error)> SendGetRequest(string url)
{
string result = string.Empty;
string error = string.Empty;
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(APIurl + url);
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
response.Content.Dispose();
response.Dispose();
result = responseContent;
}
else
{
error = "Error: " + response.StatusCode;
}
}
return (result, error);
}
public static async Task<string> SendPostRequest(string url, string jsonContent)
{
using (HttpClient httpClient = new HttpClient())
{
StringContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.PostAsync(APIurl + url, content);
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
response.Content.Dispose();
response.Dispose();
return responseContent;
}
else
{
return $"Error: {response.StatusCode}";
}
}
}
}
一边学习 一边实践 一边提升