C# .NET http,post请求
·
public static class HttpHelper
{
private static HttpClient _client;
static HttpHelper()
{
_client = new HttpClient();
}
public static async Task<string> Get(string url)
{
try
{
HttpResponseMessage response = await _client.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
Console.WriteLine($"请求出错:{ex.Message}");
return null;
}
}
public static async Task<string> Post(string url, string data, string contentType)
{
try
{
HttpContent content = new StringContent(data, Encoding.UTF8, contentType);
HttpResponseMessage response = await _client.PostAsync(url, content);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
Console.WriteLine($"请求出错:{ex.Message}");
return null;
}
}
}
更多推荐

所有评论(0)