Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么HttpURLConnection和HttpsURLConnection的下载速度不同?_Android_Inputstream_Outputstream_Httpsurlconnection - Fatal编程技术网

Android 为什么HttpURLConnection和HttpsURLConnection的下载速度不同?

Android 为什么HttpURLConnection和HttpsURLConnection的下载速度不同?,android,inputstream,outputstream,httpsurlconnection,Android,Inputstream,Outputstream,Httpsurlconnection,我正在使用HttpURLConnection或HttpsURLConnection下载内容。我的问题是为什么HttpUrlConnection和HttpsURLConnection的下载速度不同 以下是一个片段: if (downloadurl.startsWith("https://")) { HttpsConn = (HttpsURLConnection) url.openConnection(); HttpsURLConnection.setDefaultHostnameVer

我正在使用HttpURLConnection或HttpsURLConnection下载内容。我的问题是为什么HttpUrlConnection和HttpsURLConnection的下载速度不同

以下是一个片段:

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的缓冲区大小取决于我使用HttpURLConnection时分配的字节大小,下载速度更快,因为它将缓冲区数据写入文件的时间更少。但是当我使用HttpsURLConnection时,每次循环只能得到8000字节,下载速度有点糟糕

有人有主意吗?

首先,摆脱睡眠。这简直是浪费时间。它所做的只是放大了较小读取的效果,这在HTTPS中肯定会得到,因为TLS块大小限制在16k左右。关闭前刷新也是多余的,取消时您没有正确地中断循环。将循环更改为:

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

    if (isCancelled()) {
        break;
    }
}
output.close();
input.close();
。。。如果可能的话,不要在循环中做任何其他事情

设置分块流模式只会影响将请求写入连接的方式。因为你没有写任何东西,这也是毫无意义的

我不知道你说的“InputStream的缓冲区大小”是什么意思。。。取决于使用HttpURLConnection'时分配的字节大小。没有。InputStream中没有缓冲区,除非它是BufferedInputStream,并且您根本没有“分配”任何“字节大小”,除非您指的是byte[]缓冲区,它在任何意义上都不属于InputStream


您的标题与问题正文不符。

谢谢您的分享。关于如何一次性阅读16k左右的内容,你有什么建议吗?你问错了问题。您无法控制每次读取的可用数据量或SSL的速度。你要做的就是尽可能快地阅读任何到达的东西。将睡眠和其他代码添加到读取循环中无法实现这一点。