我正在开发一个需要连接到https站点的项目.每次连接时,我的代码都会抛出异常,因为该站点的证书来自不受信任的站点.有没有办法绕过证书检查.net核心http?
我在以前版本的.NET中看到了这段代码.我想我只需要这样的东西.
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
Run Code Online (Sandbox Code Playgroud) 有没有办法向 IHttpClientFactory 创建的所有客户端添加处理程序?我知道您可以对指定客户执行以下操作:
services.AddHttpClient("named", c =>
{
c.BaseAddress = new Uri("TODO");
c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
c.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue
{
NoCache = true,
NoStore = true,
MaxAge = new TimeSpan(0),
MustRevalidate = true
};
}).ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
AllowAutoRedirect = false,
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
});
Run Code Online (Sandbox Code Playgroud)
但我不想使用命名客户端,我只想向通过以下方式返回给我的所有客户端添加一个处理程序:
clientFactory.CreateClient();
Run Code Online (Sandbox Code Playgroud) 我在从我的 asp.net core 2.2 项目连接到像那里这样的 https 站点时遇到问题。我使用 IHttpClientFactory 在 Startup.cs 中创建类型化的 HttpClient
services.AddHttpClient<ICustomService, MyCustomService>();
Run Code Online (Sandbox Code Playgroud)
我不明白,如何在不像这样手动创建 HttpClient 的情况下忽略 SSL 连接问题
using (var customHandler = new HttpClientHandler())
{
customHandler.ServerCertificateCustomValidationCallback = (m, c, ch, e) => { return true; };
using (var customClient = new HttpClient(customHandler)
{
// my code
}
}
Run Code Online (Sandbox Code Playgroud)