简体   繁体   中英

The SSL connection could not be established in dotNet Core

In my microservice i am calling a third party api.

https://sms.partname.in/mspProducerM/sendSMS?queryParam

it's working fine but sometime i am getting following error.

SmsClient ? - Connection refused - System.Net.Http.HttpRequestException: Connection refused
 ---> System.Net.Sockets.SocketException (111): Connection refused
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)

I am unable to figure out the root cause. Should i bypass the certificate ? code:-

private static HttpClient _httpClient = new HttpClient();
        public static async Task<string> SendAsync(Uri url)
        {
            try
            {
                string result = string.Empty;
                using (var response = await _httpClient.GetAsync(url).ConfigureAwait(false))
                {
                    if (response != null && response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error occurred while sending sms urlLink:{url}", ex);
                throw;
            }
        }

There are a few small tweaks that you can try:

  • You should only create an HttpClient once.
    Depending on how your class is being used (where you initialized the _httpClient ), this is not the case.
  • I would change your if-statement if (response != null && response.StatusCode == System.Net.HttpStatusCode.OK) and use one of the built-in features.
    Either use response.IsSuccessStatusCode or response.EnsureSuccessStatusCode() . The latter will throw an exception in case the status code is not successful.

An other thing to take into account, is that HTTP calls will fail sometimes. This can happen for multiple reasons: network timeout, rate limiting, ... .
Perhaps you need to implement some resilience. For instance, does it work if you try the same HTTP call again, after waiting for 1 second? If so, you can easily implement this with Polly .

However, I would strongly advise that you start using the built-in features of .NET Core. Managing an HttpClient can be tricky sometimes. But .NET Core can manage this automatically, by using the IHttpClientFactory . You can even implement resilience using a few extension methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM