我有一个应用程序正在使用下面的HTTP的code,但是这正在改变到https安全方面的原因,但是这会导致下载失败。我试着只是改变了的HttpURLConnection
到 httpsURLConnection
然而,这没有奏效。
I have an app that is working using the code below for http, however for security reasons this is being changed to https, but this causes the download to fail. I tried just changing the httpURLConnection
to httpsURLConnection
however this did not work.
try {
FileOutputStream f = new FileOutputStream(directory);
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0
while ((len1 = in.read(buffer)) > 0) {
f.write(buffer, 0, len1);
Log.d("downloader","downloading");
}
f.close();
} catch (Exception e) {
e.printStackTrace();
Log.d("downloader", "catch");
}
有从我的电脑,事实上连接,如果我去到浏览器在Android手机和类型有问题的HTTPS URL加载它罚款...我只是不能计算出如何无需特殊密码或任何东西要做到这一点在我的应用程序。
there are no particular passwords or anything needed to connect from my computer and in fact if I go to the browser on the android phone and type in the HTTPS URL in question it loads it fine... I just cant figure out how to do it in my app.
我几乎没有任何安全或证书或者类似的,所以我甚至不知道这里需要什么,或去哪里找经验。
I have virtually no experience with security or certificates or any of that so I am not even sure what is needed here or where to look.
感谢
推荐答案
有一个看的 HTTPS和Android文档内SSL文章。他们在那里有一个简单的例子,以你的HTTPS证书由可信CA签名(因为你写的,你是可以使用的服务器与浏览器,你有这样的签名证书):
Have a look at the HTTPS and SSL Article within the Android documentation. They have an simple example in there, given your HTTPS certificate is signed by a trusted CA (as you write that you're able to use the server with a browser you have such a signed certificate):
URL url = new URL("https://wikipedia.org");
URLConnection urlConnection = url.openConnection();
InputStream in = urlConnection.getInputStream();
copyInputStreamToOutputStream(in, System.out);
相关推荐
最新文章