gpt4 book ai didi

c# - Azure api 请求被中止 : Could not create SSL/TLS secure channel. http 客户端在调用某些 Web api 时

转载 作者:太空狗 更新时间:2023-10-29 21:56:50 24 4
gpt4 key购买 nike

下面的代码在本地主机上运行良好,但在将我的 api 部署到 microsoft azure 上后出现上述错误。

我的代码如下:

public class UsersController : Controller   
{
private readonly HttpClient _client;

public UsersController()
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
_client = new HttpClient { BaseAddress = new Uri("https://pincode.saratchandra.in/api") };
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}

public JsonResult GetAddress(int pincode)
{
var response =_client.GetAsync("/pincode/125112").Result;

//Here it throws an exception: got no response
if (response.IsSuccessStatusCode)
{
}

return Json(ApiResult.Success(), JsonRequestBehavior.AllowGet);
}

上述调用没有得到任何响应,只是收到错误

The request was aborted: Could not create SSL/TLS secure channel

最佳答案

我们最近在 API 方面遇到了许多类似的问题。

在我们的例子中,这些问题是通过将安全协议(protocol)类型更改为 TLS 1.2 来解决的,如下所示:

  ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

这似乎是由于 SSL 被弃用以及 PCI 自 2016 年中期建议采用 TLS 1.2 的结果。请参阅 here了解更多

编辑:

这对我有用:

 class Program
{
private static HttpClient _client;
static void Main(string[] args)
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
_client = new HttpClient ();
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var response = _client.GetAsync("https://pincode.saratchandra.in/api/pincode/125112").Result;
}
}

关于c# - Azure api 请求被中止 : Could not create SSL/TLS secure channel. http 客户端在调用某些 Web api 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37324743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com