HttpURLConnection.getInputStream very slow
Asked Answered
D

3

7

HttpURLConnection.getInputStream takes very much time when compared to iPhone App which uses the same server side services.

The following code is used for the service :

         date= new java.util.Date();             
         Log.d("time","Time Stamp before posting  "+new Timestamp(date.getTime()));

         URL ur= new URL(url);           
         HttpURLConnection conn = (HttpURLConnection) ur.openConnection();
         conn.setRequestProperty("Connection", "close");
         conn.setReadTimeout(10000);
         conn.setConnectTimeout(15000);
         conn.setRequestMethod("POST");
         conn.setDoInput(true);
         conn.setDoOutput(true);             
         OutputStream os = conn.getOutputStream();
         BufferedWriter writer = new BufferedWriter(
                 new OutputStreamWriter(os, "UTF-8"));
         writer.write(getQuery(nameValuePairs));
         writer.close();
         os.close();
         conn.connect();

         StringBuffer response=null;             
         try{           
             Log.d("time","Time Stamp bfr InputStream  "+new Timestamp(date.getTime()));    

             InputStream is = conn.getInputStream();

             date= new java.util.Date();             
             Log.d("time","Time Stamp aftr InputStream  "+new Timestamp(date.getTime()));            

             BufferedReader rd = new BufferedReader(new InputStreamReader(is));
             String line;
             response = new StringBuffer(); 
             while((line = rd.readLine()) != null) {
                 response.append(line);
                 response.append('\r');
             }
             rd.close();
             response.toString();
             result=response.toString();

         } catch (Exception e) {

        }

To check where the service takes time, I put the log entries to print TimeStamp.

The average time for the process is as follows :

Average time for posting to server takes less than 2 Mil seconds
Average time for creating input stream takes almost 5 seconds

Average time for writing response is less than 2 mil seconds.

Any idea on why the input stream takes much time which makes the entire service very slow?

Despond answered 3/7, 2013 at 4:41 Comment(0)
P
1

You're not measuring what you think you're measuring. Nothing gets written to the server until you call getInputStream() or getResponseCode(). So you're really measuring:

  • connection time
  • transmission time
  • processing time at the server

when you think you're just measuring getInputStream() time.

The reason is that HttpURLConnection auto-sets the content-length header, by buffering all the output. You can avoid that by using chunked transfer mode. Then at least you will see where the time is really going.

Perjure answered 10/10, 2014 at 23:36 Comment(1)
How would we do this?Foundling
V
0

Set urlConnection.setConnectTimeout() to a lower timeout.

The class documentation for URLConnection.setConnectTimeout() says:

Sets the maximum time in milliseconds to wait while connecting. Connecting to a server will fail with a SocketTimeoutException if the timeout elapses before a connection is established. The default value of 0 causes us to do a blocking connect. This does not mean we will never time out, but it probably means you'll get a TCP timeout after several minutes.

Warning: if the hostname resolves to multiple IP addresses, this client will try each in RFC 3484 order. If connecting to each of these addresses fails, multiple timeouts will elapse before the connect attempt throws an exception. Host names that support both IPv6 and IPv4 always have at least 2 IP addresses.

I originally had mine set to urlConnection.setConnectTimeout(30000); and then changed it to urlConnection.setConnectTimeout(1000). Immediately, I saw quicker results.

Hope this helps!

Venusian answered 21/11, 2013 at 13:10 Comment(2)
You mean you saw quicker connection failures, when you got them at all. Hard to see how this solves the OP's problem.Perjure
It is strange but it works! Maybe the while loop is looping for ever or something else, but for me this answer worked perfectly!Julius
A
0

This may be related to a bug introduced in JDK 7. "HttpServer induces a 1000 ms delay when using the keep-alive cache". See:

http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8009548

Depending on your purposes, the suggested workaround is to multi-thread the HttpUrlConnection. For example, if you're using HttpServer you can do:

server.setExecutor( Executors.newFixedThreadPool( 5 ) );
Augmentation answered 14/3, 2014 at 3:46 Comment(1)
Only if he's using HttpServer at the other end. No evidence of that. And the suggested workaround is to multi-thread the server, which again is irrelevant.Perjure

© 2022 - 2024 — McMap. All rights reserved.