Error uploading file using Cordova filetransfer in

2019-07-27 06:20发布

问题:

I'm facing an issue while trying to use the Cordova File Transfer API to upload a file while the Worklight Development Server is running in HTTPS.

I created a certificate with a valid CA and put it in the server, as well as imported it to the the Android Emulator.

I'm able to login to the application and download data using the File Transfer API, but when I try to use it for uploading data I'm getting the below message in LogCat.

The code is calling a servlet implemented with our WL application to execute the download and upload.

11-26 09:20:27.854: E/FileTransfer(2362): {"target":"https:\/\/my-ip-address\/APP\/appServlet","source":"file:\/\/\/storage\/sdcard\/anywhere\/wilson\/app2\/it_1305\/1416946570255.jpg","http_status":0,"code":3}
11-26 09:20:27.854: E/FileTransfer(2362): java.io.IOException: Hostname 'my-ip-address' was not verified
11-26 09:20:27.854: E/FileTransfer(2362):   at com.squareup.okhttp.Connection.upgradeToTls(Connection.java:146)
11-26 09:20:27.854: E/FileTransfer(2362):   at com.squareup.okhttp.Connection.connect(Connection.java:106)
11-26 09:20:27.854: E/FileTransfer(2362):   at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:287)
11-26 09:20:27.854: E/FileTransfer(2362):   at com.squareup.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:248)
11-26 09:20:27.854: E/FileTransfer(2362):   at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:197)
11-26 09:20:27.854: E/FileTransfer(2362):   at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:383)
11-26 09:20:27.854: E/FileTransfer(2362):   at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
11-26 09:20:27.854: E/FileTransfer(2362):   at com.squareup.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:179)
11-26 09:20:27.854: E/FileTransfer(2362):   at org.apache.cordova.filetransfer.FileTransfer$1.run(FileTransfer.java:388)
11-26 09:20:27.854: E/FileTransfer(2362):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-26 09:20:27.854: E/FileTransfer(2362):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-26 09:20:27.854: E/FileTransfer(2362):   at java.lang.Thread.run(Thread.java:841)
11-26 09:20:27.854: E/FileTransfer(2362): Failed after uploading 0 of 18725 bytes.

回答1:

That error means that you did not use your server's host name as the CN for the certificate. Android verifies that the CN (common name) matches the host name, otherwise the certificate is not valid, as it could be any other server using your certificate to pretend that they are you (more information about this here).

If your server does not have a host name (as in hostname.com), just an IP address, you have to specify the IP in the Subject Alternative Name (SAN) as part of the certificate extenstions. Refer to this answer on how to add it as an extension. The exact procedure will depend on the tool you used to generate the certificate.

For more detailed information, look here at the android documentation on how to deal with HTTPS and SSL. Particularly, they have a section explaining how to deal with the problem that you are having, titled 'Common Problems with Hostname Verification'.