【发布时间】:2014-11-18 03:15:37
【问题描述】:
我正在使用HttpURLConnection 或HttpsURLConnection 下载内容。我的问题是为什么HttpURLConnection和HttpsURLConnection?之间的下载速度不同
这是一个sn-p:
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();
我用HttpURLConnection下载文件的时候下载速度还可以,但是用HttpsURLConnection的时候下载速度非常非常慢。我以为重点是inputStream!由于InputStream 的缓冲区大小取决于我在使用HttpURLConnection 时分配的字节大小,因此下载速度更快,因为它花费更少的时间将缓冲区数据写入文件。但是我用HttpsURLConnection的时候每次循环总是只有8000字节,下载速度有点可怕。
有人有想法吗?
【问题讨论】:
标签: android inputstream outputstream httpsurlconnection