我已经在asp.net core 2.1中创建了网站。在此网站中,我们使用httpclient调用第三方HTTPS webapi。当我在Smarterasp.net共享托管服务器上部署时,我的网站工作正常,但是当我在Windows 2016服务器上使用代理设置部署时,出现以下错误。这仅发生在HTTPS APi中。对于非https api,它可以在Windows Server 2016上正常工作。
登录失败异常System.Net.Http.HttpRequestException:发送请求时发生错误。 ---> System.Net.Http.WinHttpException:调用WINHTTP_CALLBACK_STATUS_REQUEST_ERROR的错误12175,“发生安全错误”。
在System.Threading.Tasks.RendezvousAwaitable 1.GetResult()
at System.Net.Http.WinHttpHandler.StartRequest(WinHttpRequestState state)
--- End of inner exception stack trace ---
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task
1处发送任务,HttpRequestMessage请求,CancellationTokenSource cts,布尔值disposeCts)
我已经进行了一些研究,并在此基础上设置了以下代码,但仍然出现错误。对于非HTTPs api可以正常工作。
AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);
var handler = new HttpClientHandler();
if (ProxySettings.Current.UseProxy)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
handler.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
{
return true;
};
var proxy = new WebProxy();
if (!string.IsNullOrWhiteSpace(ProxySettings.Current.ProxyHost) && ProxySettings.Current.ProxyPort != 0)
{
proxy = new WebProxy(ProxySettings.Current.ProxyHost, ProxySettings.Current.ProxyPort);
}
proxy.UseDefaultCredentials = ProxySettings.Current.UseDefaultCredentials;
if (!string.IsNullOrWhiteSpace(ProxySettings.Current.ProxyUrl))
proxy.Address = new Uri(ProxySettings.Current.ProxyUrl);
if (ProxySettings.Current.HasNetworkCredentials)
{
var credentials = new NetworkCredential();
credentials.UserName = ProxySettings.Current.NetworkUserName;
credentials.Password = ProxySettings.Current.NetworkPassword;
if (!string.IsNullOrWhiteSpace(ProxySettings.Current.NetworkDomain)) credentials.Domain = ProxySettings.Current.NetworkDomain;
proxy.Credentials = credentials;
}
handler = new HttpClientHandler
{
UseProxy = true,
Proxy = proxy,
};
}
能帮您解决问题吗?