开发者

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

I'm using the Apache DefaultHttpClient in an Android app to make a get request to a webserver (URL doesn't matter, the phenomenon is seen for different servers/websites). The first request using the HTC Desire with the 3G network takes several seconds (>4s), subsequent ones roughly only 0.5s. If I wait more than ~15 seconds before making the next request, this next request again needs several seconds. The request is using HTTP1.1, the server time is not the problem. DNS lookup is also not the problem, as I tried using the IP as well.

For me it looks like either some connection timeout setting in the Apache framework or my HTC device putting the network i开发者_C百科nterface to sleep (if it does this 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 for the reason or experience the same thing? I already put this into an AsyncTask, but still I'd like to know why it is this slow.

Thanks, Martin


There are a couple of options here:

  • Keepalives: It might be that it's expensive to set up the initial TCP connection then reusing the tcp connection for subsequent http requests. This explains the tcp connection being dropped after ~10-15 seconds forcing you to re-initiate them

  • DNS cache expiry - very unlikely since we are talking of seconds instead of minutes.

The best thing to do would be to set up tcpdump and capture packets. You can do this by capturing the packets inbound on your server and having a look at the tcp stream.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜