Hardik Patel
Hardik Patel

Reputation: 178

First request with HttpClient on Android takes more than 45 seconds, after that less than second

On Android, I have seen that Http Client takes longer for first request (around 45 to 65 seconds) and then onward, It is always less than second Do you know why? How can we have same response time for first request like any other requests?

Here is the sample code

public class HttpClientTest
{
    private HttpClient _httpClient;
    public HttpClientTest()
    {
        HttpClientHandler hch = new HttpClientHandler
        {
            UseProxy = false,
        };
        _httpClient = new HttpClient(hch)
        {
            Timeout = new TimeSpan(0, 0, 0, 100)
        };
    }

    public async Task TestHttpClientAsync(string key, string url)
    {
        DateTime startTime = DateTime.Now;
        Console.WriteLine(string.Empty);
        Console.WriteLine($"TestHttpClientAsync : Start : {key} : {startTime}");
        var result = await _httpClient.GetByteArrayAsync(url).ConfigureAwait(false);
        DateTime endTime = DateTime.Now;
        Console.WriteLine(string.Empty);
        Console.WriteLine($"TestHttpClientAsync : End    : {key} : {endTime.Subtract(startTime).TotalMilliseconds}");
        Console.WriteLine(string.Empty);
        Console.WriteLine($"TestHttpClientAsync : Total  : {key} : {endTime} with {result?.Length}");
        Console.WriteLine(string.Empty);
    }
}

Here is how, I am calling it from constructor of MainPage of Shared Code

        HttpClientTest testHttpClient = new HttpClientTest();
        Task.Run(() => testHttpClient.TestHttpClientAsync("Google","https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png")).Wait();
        Task.Run(() => testHttpClient.TestHttpClientAsync("Microsoft","https://assets.onestore.ms/cdnfiles/external/uhf/long/9a49a7e9d8e881327e81b9eb43dabc01de70a9bb/images/microsoft-gray.png")).Wait();
        Task.Run(() => testHttpClient.TestHttpClientAsync("Chrome","https://www.google.com/chrome/assets/common/images/chrome_logo_2x.png?mmfb=a5234ae3c4265f687c7fffae2760a907")).Wait();

Here is the result of this code See line begining with "TestHttpClientAsync : End :" to see response time

TestHttpClientAsync : Start  : Google : 24/01/2018 19:39:30
TestHttpClientAsync : End    : Google : 4610.646
TestHttpClientAsync : Total  : Google : 24/01/2018 19:39:35 with 5969
TestHttpClientAsync : Start  : Microsoft : 24/01/2018 19:39:35
TestHttpClientAsync : End    : Microsoft : 681.655
TestHttpClientAsync : Total  : Microsoft : 24/01/2018 19:39:36 with 4054
TestHttpClientAsync : Start  : Chrome : 24/01/2018 19:39:36
TestHttpClientAsync : End    : Chrome : 100.584
TestHttpClientAsync : Total  : Chrome : 24/01/2018 19:39:36 with 5666

Upvotes: 0

Views: 530

Answers (1)

cansado2930
cansado2930

Reputation: 339

I think that problem is with DNS, I recommend that you try to set other DNS as 8.8.8.8 and try it again. And there are different technics for resolving DNS (to search the near server) and it can require more time, and then the next request, it knows what is the server (while resolved DNS is valid) so the request is more short . And there are more factors on connection on the internet. In my test with my 4 different servers, I do not see it, although I'm using ModernHttpClient plugin that has more features than basic. you can try it too

Upvotes: 1

Related Questions