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?