0

I am using HttpURLConnection or HttpsURLConnection for downloading content. My question is why is the download speed different betweenHttpURLConnection and HttpsURLConnection?

Here is a snippet :

if (downloadurl.startsWith("https://")) {
   HttpsConn = (HttpsURLConnection) url.openConnection();
   HttpsURLConnection.setDefaultHostnameVerifier(new AllowAllHostNameVerifier());       

    SSLContext sc;
                sc = SSLContext.getInstance("TLS");
                sc.init(null, new TrustManager[] {
                          new X509TrustManager() {
                                public void checkClientTrusted(X509Certificate[] chain, String authType) {}
                                public void checkServerTrusted(X509Certificate[] chain, String authType) {}
                                public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; }
                              }
                            }, null);
                HttpsConn.setSSLSocketFactory(sc.getSocketFactory());
   HttpsConn.setSSLSocketFactory(sc.getSocketFactory());

   HttpsConn.setConnectTimeout(CONNECT_TIME_SECONDS * 1000);
   HttpsConn.setReadTimeout(READ_TIME_SECONDS * 1000);
   HttpsConn.setChunkedStreamingMode(0);  
   HttpsConn.connect();
} else {
   URLConn = (HttpURLConnection) url.openConnection();
   URLConn.setConnectTimeout(CONNECT_TIME_SECONDS * 1000);
   URLConn.setReadTimeout(READ_TIME_SECONDS * 1000);
   URLConn.setChunkedStreamingMode(0);  
   URLConn.connect();
}               

                                   .
                                   .
                                   .

byte data[] = new byte[1048576];
double currentDownloadSize = 0.0;
long startTime = System.currentTimeMillis();
lastUpdateTime = startTime;
int count;

while ((count = input.read(data)) != -1) {
    currentDownloadSize += count;
    output.write(data, 0, count);
        Thread.sleep(10);

    if (isCancelled()) {
        output.flush();
        output.close();
        input.close();
    }
}

output.flush();
output.close();
input.close();

The download speed is OK when I use HttpURLConnection to download a file, but when I use HttpsURLConnection, the download speed is very very slow. I thought the key point is inputStream! Since the buffer size of InputStream depends on the byte size I assigned when I use HttpURLConnection, the download speed is faster since it spends less time at writing the buffer data to file. But I always only got 8000 bytes every loop when I use HttpsURLConnection, the download speed is a little horrible.

Anyone got ideas?

user207421
  • 305,947
  • 44
  • 307
  • 483
梁峻騰
  • 1
  • 3

1 Answers1

0

Get rid of the sleep(), for a start. It is just literally a waste of time. All it does is magnify the effect of smaller reads, which you will certainly get with HTTPS as there is a TLS block size limit of around 16k. flush() before close() is also redundant, and you aren't breaking out of your loop correctly on cancellation. Change your loop to this:

while ((count = input.read(data)) != -1) {
    currentDownloadSize += count;
    output.write(data, 0, count);

    if (isCancelled()) {
        break;
    }
}
output.close();
input.close();

... and don't do anything else in the loop if possible.

Setting chunked streaming mode only affects how you write the request to the connection. As you aren't writing anything, this is also pointless.

I don't know what you mean by 'the buffer size of InputStream ... depends on the bytes size I assigned when I use HttpURLConnection'. It doesn't. There is no buffer in an InputStream unless it's a BufferedInputStream, and you didn't 'assign' any 'bytes size' at all, unless you mean the byte [] buffer, which doesn't belong in any sense to the InputStream.

Your title doesn't agree with the body of your question.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thank you for your sharing. Do you have any suggestions about how to read around 16k in one time? – 梁峻騰 Nov 18 '14 at 08:14
  • You're asking the wrong question. You can't control how much data is available per read,or the speed of SSL. What you have to do is read whatever arrives as fast as possible. Adding sleeps and other code into the read loop won't accomplish that. – user207421 Nov 18 '14 at 08:46