public static async Task<string> sendPostAsync(string url,Dictionary<string,string> headers, string requestBody = null)
{
try
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
//添加请求头
if(headers != null)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key,header.Value);
}
}
HttpContent content = new StringContent(requestBody, Encoding.UTF8, "application/json");
request.Content = content;
HttpClientHandler handler = new HttpClientHandler();
//关键步骤
//跳过ssl认证
handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
HttpClient httpClient = new HttpClient(handler);
HttpResponseMessage responseMessage = await httpClient.SendAsync(request);
string responseContent = await responseMessage.Content.ReadAsStringAsync();
Console.WriteLine(responseContent);
return responseContent;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return string.Empty;
}
}
使用HttpClient发送https请求时跳过ssl证书认证
最新推荐文章于 2024-04-13 00:54:06 发布