问题
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 secondsAverage 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?
回答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.
回答2:
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!
回答3:
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 ) );
来源:https://stackoverflow.com/questions/17439802/httpurlconnection-getinputstream-very-slow