Xamarin.Android HTTP random delays

I have a method that performs several HTTP requests called LoadServers()A, which performs LoadServers(). For my problem, I only press the button after completing the previous call LoadServers(). During loading, a run dialog appears, so I can only execute them sequentially.

After about every 10-15 calls, the LoadServersfirst HTTP request is delayed for almost exactly 10 seconds. The average completion time LoadServersis less than half a second and not more than 1 second. This only happens on Xamarin.Android. No delay occurs on Xamarin.iOS, and all this code is shared.

Here is my code

private async Task LoadServers() {
    await Get();
    await Post();
    await Get();
    await Get();
    await Post();
}

private async Task Get() {
    var url = _httpClient.BaseAddress + model.GetToken();
    Log("Attempting to send GET to: " + url);
    using (var response = await _httpClient.GetAsync(url))
    {
        var resultContent = await response.Content.ReadAsStringAsync();
        Log("Got response back from : " + url + ": " + resultContent);
    }
}

private async Task Post() {
    var content = requestData.GetToken() + "=" + requestData.PostBody ();
    var request = new StringContent(content)
    {
        Headers = { ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") }
    };
    var url = _httpClient.BaseAddress + "/No_content";
    Log ("Attempting to send POST to: " + url + "  with content: " + content);
    using (var response = await _httpClient.PostAsync(url, request))
    {
        string resultContent = await response.Content.ReadAsStringAsync();
        Log("Got response back from : " + url + ": " + resultContent);
    }
}

Every 15 or so runs LoadServers()result in the following log statements:

Thread started:  #37
Thread finished:  #37
[2017-01-21T13:42:30.8841620-06:00] [debug] Loading Servers
[2017-01-21T13:42:30.8946770-06:00] [debug] Attempting to send GET to: XXX
Thread finished: <Thread Pool> #23
Thread started: <Thread Pool> #38
[2017-01-21T13:42:40.9550360-06:00] [debug] Got response back from : XXX <-- Notice the time (~10 seconds)

HTTP-? , ? , , . ?

+4
2

, . System.Net MessageHandler HttpClient . , , , .

ModernHttpClient ( Xamarin Nuget | | Github) NativeMessageHandler HttpClient. ( Android, OkHttp), .

+2

. xamarin.android 6.6.1.2-21 Api 23.

, httpHandler 2 httpClients .

new HttpClient( new Xamarin.Android.Net.AndroidClientHandler());

2 httpclients .

"HttpClient" "AndroidClientHandler". : a > Android build > . android 5 +

http

0

Source: https://habr.com/ru/post/1667452/


All Articles