0

I have implemented httpClientFactory but can't connect to the backend api using broken ssl.I tried a lot and i just got the task cancelled error -> HttpClient - A task was cancelled?

I have tried to set this

        public static HttpClient getHttpClient()
        {
            if (_httpClient == null)
            {
                Uri baseUri = new Uri(Url.baseUrl);
                if (baseUri.Scheme == "http")
                {
                    var handler = new HttpClientHandler();
                    handler.ClientCertificateOptions = ClientCertificateOption.Manual;
                    handler.ServerCertificateCustomValidationCallback =
                        (httpRequestMessage, cert, cetChain, policyErrors) =>
                        {
                            return true;
                        };
                    _httpClient = new HttpClient(handler);
                }
                else
                {
                    _httpClient = new HttpClient();
                }
                _httpClient.DefaultRequestHeaders.Accept.Clear();
                _httpClient.DefaultRequestHeaders.Accept.Add(new
                MediaTypeWithQualityHeaderValue("application/json"));
                _httpClient.BaseAddress = baseUri;
            }
            return _httpClient;
        }
Sandip124
  • 3
  • 5
  • Your logic is back to front. You need the certificate stuff for HTTPS, not HTTP. – user207421 Dec 23 '19 at 09:15
  • What exactly is *"broken ssl"*? There is no single cure for every kind of SSL problems so you need to provide details on what exactly the SSL problem is. – Steffen Ullrich Dec 23 '19 at 09:43
  • Actually the following http client works on the https but when there is situation wheren the ssl certificate is expired or something else then while connecting to http i got thre error task canceled. so i just need simple logic that if the site has broken ssl the instead of throwing error i should successfully get connected ignoring the ssl certificate. – Sandip124 Dec 24 '19 at 04:49

0 Answers0