我为客户开发了WPF应用程序。我在Windows 10上使用.NET Framework 4.7.2在Visual Studio 2019上开发了它。
客户有不同的操作系统,例如Windows 7 SP 1:问题是此操作系统。
当我在Windows 7上运行该应用程序时,一切正常,但在“登录”表单上,HttpRequest发生异常,并出现此错误
System.AggregateException
HResult=0x80131500
Message=One or more errors occurred.
Source=mscorlib
StackTrace:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at Project.Services.HttpClientService.Post[T](String server, String url, Object content, AuthenticationType authenticationType, String token, String apiKey) in C:\DEV\project\class.cs:line 65
This exception was originally thrown at this call stack:
[External Code]
Inner Exception 1:
HttpRequestException: An error occurred while sending the request.
Inner Exception 2:
WebException: Unable to connect to the remote server
Inner Exception 3:
SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 10.*.*.*:8080
[我隐藏了有意义的数据,例如项目名称或IP]
我阅读了Microsoft提供的一些文档,并尝试了该文档,例如SSL / TLS协议修复或停用了防火墙
这是我用来检索数据的代码
public T Post<T>(string server, string url, dynamic content, EnumDictionary.AuthenticationType authenticationType, string token = null, string apiKey = null)
{
using (var httpClient = new HttpClient())
{
new HttpClientHandler { ClientCertificateOptions = ClientCertificateOption.Automatic };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
httpClient.BaseAddress = new Uri(server);
switch (authenticationType)
{
case EnumDictionary.AuthenticationType.Basic:
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", token);
break;
case EnumDictionary.AuthenticationType.Bearer:
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
break;
case EnumDictionary.AuthenticationType.None:
break;
default:
break;
}
if (apiKey != null)
httpClient.DefaultRequestHeaders.Add("APIKey", apiKey);
HttpResponseMessage result;
try
{
result = httpClient.PostAsync(url, new StringContent(content.ToString(), Encoding.UTF8,
"application/json")).Result;
}
catch (SocketException sexc)
{
throw new Exception("Socket Error");
}
if (result.IsSuccessStatusCode)
{
var resultString = result.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<T>(resultString);
}
else
{
if (result.StatusCode == System.Net.HttpStatusCode.Unauthorized)
throw new UnauthorizedException();
throw new Exception("Call failure");
}
}
}
我使用从此帖子中找到的代码进行修复
[Obsolete("Do not use this in Production code!!!",true)]
static void NEVER_EAT_POISON_Disable_CertificateValidation()
{
// Disabling certificate validation can expose you to a man-in-the-middle attack
// which may allow your encrypted message to be read by an attacker
// https://stackoverflow.com/a/14907718/740639
ServicePointManager.ServerCertificateValidationCallback =
delegate (
object s,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors
) {
return true;
};
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句