Home > Mobile >  In .net5.0 - Extranal API response time takes too long time - httpclient
In .net5.0 - Extranal API response time takes too long time - httpclient

Time:09-23

I'm trying to get response a external API using httpclint in .netcore5.0.

Initially I got timeout exception. So I add client.Timeout = Timeout.InfiniteTimeSpan; after adding this response is come. but it takes more than 20 mins.

But I browser I can get API result within milliseconds.

How can I get response from API with a short time. Any idea to decrease this responding time?

startup.cs

 services.AddHttpClient<IHolidayService, HolidayService>("PublicHolidaysApi", c => c.BaseAddress = new Uri("https://api.xmltime.com"));

service.cs

public class HolidayService : IHolidayService
    {
        private readonly IHttpClientFactory _clientFactory;

        private readonly HttpClient _client;
        public HolidayService(HttpClient client)
        {
            _client = client;
            client.Timeout = Timeout.InfiniteTimeSpan;
        }
        public  HolidayService(IHttpClientFactory clientFactory)
        {
            _clientFactory = clientFactory;
            _client = clientFactory.CreateClient("PublicHolidaysApi");
        }

        public async Task<Holiday> GetHolidays(string country,int year)
        {
            string url = string.Format($"/holidays?accesskey=dYjG8ztthf&secretkey=veUV06dNmrnp7bbaYq0u&version=3&country=ro&year=2021&lang=en");
            var result = new Holiday();

            using (var cts = new CancellationTokenSource(Timeout.InfiniteTimeSpan))
            {
                var response =  await _client.GetAsync(url, cts.Token).ConfigureAwait(false);


                if (response.IsSuccessStatusCode)
                {
                    using var responseStream = await response.Content.ReadAsStreamAsync();
                    result = await JsonSerializer.DeserializeAsync<List<Holiday>>(responseStream);

                }
                else
                {
                    throw new HttpRequestException(response.ReasonPhrase);
                }
            }
  return result;
        }
    }
}

CodePudding user response:

There are so many possibilities in this situation and I can only give you a way to solve the problem.

First of all, we need to locate the reason why it is so slow. Is it the server or the client?

We can use packet capture tools such as Fiddler ,and then observe the corresponding network requests.

If client had send but server not response , you should think about the api limit... And if not, the request are not send at all, may be you should the check the connection pool of the HttpClient, or the WorkThreadPool of dotnet.

CodePudding user response:

there are a few problems with your question.

  1. it doesn't compile.
  2. it is incomplete.
  3. it has sensitive data.

but I can get the data from API in no time. just open this link https://dotnetfiddle.net/ryjakT and run the program.
I changed few things

  1. Return Type, it should be List
  2. var result = new Holiday(); to var result = new List();
  3. I am using Newtonsoft.Json for Deserialization.
  4. you were trying to Deserialize to an incorrect model, it should be Root.
  • Related