SocketException: 连接尝试失败,因为连接方在一段时间后没有正确响应 Windows 7 SP 1

SocketException: A connection attempt failed because the connected party did not properly respond after a period of time on Windows 7 SP 1

我为客户开发了一个 WPF 应用程序。我在 Visual Studio 2019 年使用 .NET Framework 4.7.2 在 Windows 10.

上开发了它

客户有不同的OS,比如Windows 7 SP 1:问题是这个OS。

当我 运行 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");
                }
            }
        }

我使用从这个 post

中找到的代码进行修复

The remote certificate is invalid according to the validation procedure

[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
    // 
    ServicePointManager.ServerCertificateValidationCallback =
        delegate (
            object s,
            X509Certificate certificate,
            X509Chain chain,
            SslPolicyErrors sslPolicyErrors
        ) {
            return true;
        };
}