Http request is slow for the first time and waiting too long

I use Apache DefaultHttpClient in an Android application to receive a request to a web server (the URL does not matter, the phenomenon is visible for different servers / websites). The first request using HTC Desire with a 3G network takes a few seconds (> 4 s), and the next request takes about 0.5 s. If I wait more than 15 seconds before the next request, the next request will again take several seconds. The request uses HTTP1.1, server time is not a problem. Finding DNS is also not a problem, as I also tried to use IP.

For me, it looks like either some connection timeout parameter in the Apache infrastructure, or my HTC device, which puts the network interface into sleep mode (if it does at all).

The coding is pretty simple, it looks like this:

HttpGet get = new HttpGet(uri); long startTimeMillisRequest = System.currentTimeMillis(); HttpResponse response = client.execute(get); long endTimeMillisRequest = System.currentTimeMillis(); 

Does anyone have a clue to the cause or experience of the same? I already put this in AsyncTask, but still I would like to know why it is so slow.

Thanks Martin

+4
source share
1 answer

There are several options here:

  • Keepalives: It may be expensive to set up the initial TCP connection and then reuse the tcp connection for subsequent HTTP requests. This explains that the tcp connection is dropped after ~ 10-15 seconds, forcing you to re-initiate them.

  • The duration of the DNS cache is very unlikely, since we are talking about seconds instead of minutes.

It would be best to tcpdump and capture packages. You can do this by capturing incoming packets on your server and looking at the tcp stream.

+2
source

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


All Articles