Xamarin Android First web request deadly

The first call I made for my Android Xamarin application is deadly slow, requiring 10 times the required time.

I have already tried all coding options like "HttpClient", "WebClient" etc.

For example:

public string MakeWebRequest(string url)
{
    try
    {
        string responseContent;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";
        request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);
        request.Proxy = GlobalProxySelection.GetEmptyWebProxy(); // null;

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (Stream responseStream = response.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(responseStream))
                    responseContent = sr.ReadToEnd();
            }
        }

        return responseContent;
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

I tried the same code from the Windows Phone App and it works in 1/10 of the time. Console application, the same result. I have tried this also on many devices without success.

All other queries work well.

How can I solve it?

+4
source share

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


All Articles