Minifycode 2022-04-19 Viewed 1.2K times ASP.Net Core MVC

In this article you will learn how to resolve The SSL connection could not be established .Net Core. 

Yes, you can Bypass the certificate using below code...

 

public async Task<ApiResponse> HTTPPost(string requestString, string url)
        {
            
            try
            {
                CollectTransactionRequestFinal c = new CollectTransactionRequestFinal()
                {
                    requestMsg = requestString,
                    pgMerchantId = <Your Key>
                };
                var bodyJson = JsonConvert.SerializeObject(c);
                var stringContent = new StringContent(bodyJson, Encoding.UTF8, "application/json");
                using (var handler = new HttpClientHandler())
                {
                    // allow the bad certificate
                    handler.ServerCertificateCustomValidationCallback = (request, cert, chain, errors) => true;
                    using (var httpClient = new HttpClient(handler))
                    {
                       var r= await httpClient.PostAsync(url, stringContent);
                       var result = await r.Content.ReadAsStringAsync();
                        return new ApiResponse
                        {
                            statusCode = "",//response.StatusCode.ToString(),
                            Response = result
                        };
                    }
                }
                
            }
            catch (Exception ex)
            {
                
            }
        }

 

Other ways....

 

HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };

// Pass the handler to httpclient(from you are calling api)
HttpClient client = new HttpClient(clientHandler);

The SSL connection could not be established .Net Core.
minify code