Reputation: 53
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;
}
}
Upvotes: 0
Views: 2112
Reputation: 305
There are a few small tweaks that you can try:
_httpClient
), this is not the case.if (response != null && response.StatusCode == System.Net.HttpStatusCode.OK)
and use one of the built-in features.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.
Upvotes: 1