The first HTTP POST and GET attempts are always slow - is this OS or network connected?

Now I just started uploading my legs to HTTP. I used simple HTTP requests using GET and POST. The webpage I used is a 3-line php check for the correct $ _GET [] and $ _POST [], and then just the echo character "1". I use POST and GET with the same short pair of names / values, in the hope that packet fragmentation is not needed, and all this is done in a thread that is disconnected from the user interface thread. Requests are looped on the phone several times, synchronizing them. Everything works well. (See code below) That is, I return the answer "1". But there is a constant question of time. I observe that:

  • In the first attempt, the query execution time is much longer than subsequent attempts in both the GET and POST methods.

  • Other attempts are always much faster for both.

  • GET is always faster than POST.

All this is true for 3G and Wifi connections (but with Wi-Fi much faster, as expected).

I tried this with BasicResponseHandler () and with more manual buffered I / O methods with the same results.

I believe that I understand 3. as a result of the fact that POST requires two transfers, one for "HTTP 100" and then the body of the packet. - Is it correct?

My main question is what happens in the first request attempts, which are so slow? Sometimes it takes a few seconds (!). Is it a network that holds things or Android, putting it on a socket, creating some kind of queue? If it is Android, is there a way to code and avoid this more correctly? Is there something in keeping the socket open to make this problem only once at runtime? If so, is this good practice? Admittedly, this is apsect, which I don't know most about.

I found some discussion on this, but no one got into this aspect directly.

-

The basic code for the GET and POST methods looks like this (minus try / catch) and is executed in the thread from the user interface, first the GET method, then the POST method: (output below)

GET part of it:

public String[] HTTPGETIt(int numrounds) { HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter (CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1); HttpGet GETRequest = new HttpGet("http://mypage.com/epoch.php?mynameis=tam"); ResponseHandler <String> MyBRH = new BasicResponseHandler(); String[] GETResult = new String[numrounds]; int i = 0; long timestart, DT; while(i < numrounds) { timestart = System.currentTimeMillis(); GETResult[i] = httpclient.execute(GETRequest, MyBRH); DT = System.currentTimeMillis() - timestart; Log.d(TAG, "(" + i + ") GET-Round Trip was "+ DT + " ms."); i++; }//while i <= numrounds httpclient.getConnectionManager().shutdown(); return GETResult; } //END HTTPGETIt 

And the POST version:

 public String[] HTTPPOSTIt(int numrounds) { String Place = "HTTPPostping"; HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1); HttpPost PostRequest = new HttpPost("http://mypage.com/epoch.php"); ResponseHandler <String> MyBRH = new BasicResponseHandler(); String[] POSTResult = new String[numrounds]; List<NameValuePair> MynameValuePairs = new ArrayList<NameValuePair>(2); MynameValuePairs.add(new BasicNameValuePair("mynameis", "tam")); PostRequest.setEntity(new UrlEncodedFormEntity(MynameValuePairs)); int i = 0; long timestart, DT; while(i < numrounds) { timestart = System.currentTimeMillis(); POSTResult[i] = httpclient.execute(PostRequest, MyBRH); DT = System.currentTimeMillis() - timestart; Log.d(TAG, "(" + i + ") POST-Round Trip was "+ DT + " ms."); i++; }//while i <= numrounds httpclient.getConnectionManager().shutdown(); return POSTResult; } // END HTTPPOSTIt 

They cause:

 Runnable HTTPGETJob = new HTTPGETTask(NS); Thread HTTPGETThread = new Thread(HTTPGETJob, "HTTPGETThread"); HTTPGETThread.setPriority(Thread.MAX_PRIORITY); HTTPGETThread.start(); 

and

 Runnable HTTPPOSTJob = new HTTPPOSTTask(NS); Thread HTTPPOSTThread = new Thread(HTTPPOSTJob, "HTTPPOSTThread"); HTTPPOSTThread.setPriority(Thread.MAX_PRIORITY); HTTPPOSTThread.start(); 

With runnables:

 class HTTPGETTask implements Runnable { int numtimes; DeviceInfo tsrtDI; HTTPGETTask(int inNS) { this.numtimes = inNS; } @Override public void run() { long [] TT2NS = new long[numtimes]; TT2NS = HTTPGETIt(numtimes); } }; 

and

 class HTTPPOSTTask implements Runnable { int numtimes; DeviceInfo tsrtDI; HTTPPOSTTask(int inNS) { this.numtimes = inNS; } @Override public void run() { long [] TT2NS = new long[numtimes]; TT2NS = HTTPPOSTIt(numtimes); } }; 

The output is usually:

(0) GET-Round Trip was 368 ms.

(1) The GET-Round Trip was 103 ms.

(2) The GET-Round Trip was 98 ms.

(3) The GET-Round Trip was 106 ms.

(4) The GET-Round Trip was 102 ms.


(0) POST-Round Trip was 1289 ms.

(1) POST-Round Trip was 567 ms.

(2) POST-Round Trip was 589 ms.

(3) POST-Round Trip was 496 ms.

(4) POST-Round Trip was 557 ms.

+6
source share
2 answers

I would insist on installing the Protocol Version as HTTP 1.1 and give it a try. This would increase the request / response time than now. I have not tried it yet, I just received the information. So, before executing the request, you can try something like below.

 httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 
+1
source

I agree with the Lalit Poptani Answer, which can help speed up the call, because it seems to be because HTTP 1.1 supports the connection and does not perform hand shake every time.

But at the same time, I would like to say that this is not an Android-specific problem, this same thing happens on almost every platform and in every language, because the first time you make any HTTP call that the platform needs to create and set different objects for different objects that assign values ​​to it to make a call, and which remains there, so when you make a new call, the time for creating and assigning objects is saved, so you get faster answers.

What are these objects? - this is stated in the following lines: I do not have a clear list, but they are similar to the proxy server settings, cache for it.


Wait a few more until someone comes with in-depth technical knowledge about this and explains how everything works for.

+1
source

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


All Articles