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

1

I developed a WPF Application for a customer. I developed it on Visual Studio 2019 with .NET Framework 4.7.2 on Windows 10.

The customer have different OS, like Windows 7 SP 1: the problem is this OS.

When I run the application on Windows 7, everything works fine, but on Login form, the HttpRequest goes on exception with this error

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

[ I hide the sensible data, like project name or IP ]

I read some doc provided by Microsoft, and tried it, like SSL/TLS Protocol Fix, or deactivating the Firewall

This is the code that I use to retrieve the data

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");
                }
            }
        }
c#
wpf
http
dotnet-httpclient
asked on Stack Overflow Nov 27, 2020 by Teo230 • edited Nov 27, 2020 by Teo230

1 Answer

1

I fix using the code found from this 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
    // https://stackoverflow.com/a/14907718/740639
    ServicePointManager.ServerCertificateValidationCallback =
        delegate (
            object s,
            X509Certificate certificate,
            X509Chain chain,
            SslPolicyErrors sslPolicyErrors
        ) {
            return true;
        };
}
answered on Stack Overflow Dec 3, 2020 by Teo230

User contributions licensed under CC BY-SA 3.0