gpt4 book ai didi

c# - Webapi http 调用导致 SSL 异常

转载 作者:太空宇宙 更新时间:2023-11-03 12:06:16 28 4
gpt4 key购买 nike

我正在使用一个名为 Refit 的库访问 RESTful 服务

public interface IApiClient
{
[Get("/{Id}/shifttimes")]
Task<ShiftTimesResponse> GetShiftTimes(int id);
}

我正在 DI 中初始化接口(interface):

builder.Register(ctx =>
{
var baseUri = new Uri(config.ApiUri);
var client = RestService.For<IApiClient>(baseUri.OriginalString);
return client;

}).As<IApiClient>().InstancePerTenant();

然后在 myHandler 中使用:

public class ShiftTimesHandler
{
private readonly IApiClient _apiClient;

public ShiftTimesHandler(
IApiClient apiClient)
{
_apiClient= apiClient;
}

public async Task Execute(Message message)
{
**Error on this line**
var shiftTimesResponse = await _apiClient.GetShiftTimes(message.Id);

//Do something
}
}

异常(exception)是:

The SSL connection could not be established, see inner exception.

内部异常是:

The remote certificate is invalid according to the validation procedure.: AuthenticationException

最佳答案

下面为我工作。但是,它不应该在生产环境中完成:

            var httpClientHandler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
};

var httpClient = new HttpClient(httpClientHandler)
{
BaseAddress = new Uri(config.ApiUri);
};

return RestService.For<IHoursApiClient>(httpClient);

关于c# - Webapi http 调用导致 SSL 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54803301/

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