我无法理解为什么我的 Android 服务器在 HTTPS 中比在 HTTP 中慢得多。正如您从下面的卷曲中看到的那样,我认为这不是由于握手,因为握手发生得相对较快
thomas-mba:thomas$ curl -v --trace-time --insecure https://192.168.1.19:8101/tasks
16:59:10.719547 * About to connect() to 192.168.1.19 port 8101 (#0)
16:59:10.720632 * Trying 192.168.1.19...
16:59:10.750861 * connected
16:59:10.750952 * Connected to 192.168.1.19 (192.168.1.19) port 8101 (#0)
16:59:10.751810 * SSLv3, TLS handshake, Client hello (1):
16:59:11.666648 * SSLv3, TLS handshake, Server hello (2):
16:59:11.666737 * SSLv3, TLS handshake, CERT (11):
16:59:11.714497 * SSLv3, TLS handshake, Server finished (14):
16:59:11.714822 * SSLv3, TLS handshake, Client key exchange (16):
16:59:11.714887 * SSLv3, TLS change cipher, Client hello (1):
16:59:11.715034 * SSLv3, TLS handshake, Finished (20):
16:59:11.725787 * SSLv3, TLS change cipher, Client hello (1):
16:59:11.725959 * SSLv3, TLS handshake, Finished (20):
16:59:11.726056 * SSL connection using AES128-SHA
16:59:11.726114 * Server certificate:
16:59:11.726184 * subject: CN=192.168.1.19
16:59:11.726247 * start date: 2013-10-03 14:58:20 GMT
16:59:11.726302 * expire date: 2014-10-03 14:59:10 GMT
16:59:11.726362 * common name: 192.168.1.19 (matched)
16:59:11.726423 * issuer: CN=My CA name
16:59:11.726472 * SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway.
16:59:11.726609 > GET /tasks HTTP/1.1
16:59:11.726609 > User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5
16:59:11.726609 > Host: 192.168.1.19:8101
16:59:11.726609 > Accept: */*
16:59:11.726609 >
16:59:22.310511 < HTTP/1.1 200 OK
16:59:22.310610 < Date: Thu, 03 Oct 2013 14:59:21 GMT+00:00
16:59:22.310645 < Server: My HTTP Server
16:59:22.310680 < Content-Length: 4248
16:59:22.310714 < Content-Type: text/html
16:59:22.310749 <
最耗时的是绑定socket:
// Set up HTTP connection
Socket socket = this.mServerSocket.accept();
socket.setKeepAlive(true);
DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
Log.d(TAG,String.format("%s || Incoming connection from %s",
new Date().toString(),
socket.getInetAddress()));
conn.bind(socket, mParams);
Log.d(TAG,String.format("%s || Bind finished",
new Date().toString()));
// Start worker thread
Thread t = new WorkerThread(this.mHttpService, conn, socket);
t.setDaemon(true);
t.start();
Log.d(TAG,String.format("%s || Worker thread started",
new Date().toString()));
以上输出:
Thu Oct 03 17:10:05 CEST 2013 || Incoming connection from /192.168.1.29
GC_FOR_ALLOC freed 334K, 7% free 8987K/9568K, paused 55ms, total 58ms
Thu Oct 03 17:10:29 CEST 2013 || Bind finished
Thu Oct 03 17:10:29 CEST 2013 || Worker thread started
你认为原因是什么?
编辑:
在 HTTP 中也是一样的:
Thu Oct 03 17:23:25 CEST 2013 || Incoming connection from /192.168.1.29
Thu Oct 03 17:23:25 CEST 2013 || Bind finished
Thu Oct 03 17:23:25 CEST 2013 || Worker thread started
我不敢相信加密一个 4KB 的 html 页面需要 25 秒而不是 1 秒,尤其是当它到处都说 HTTPS 与 HTTP 相比不再那么慢时。