Android DownloadManager and SSL (https)
Asked Answered
M

3

20

Oh, great. There's always something else that some... Grrr...

Anyway, so I worked days and days wading through vague, incomplete, and contradictory Picasa information so that my Android app could find a Picasa picture and download it using the download manager. So just now I made the finishing touches and hit the "run" button. Everything went fine until DownloadManager tried to download the file:

java.lang.IllegalArgumentException: Can only download HTTP URIs: https://example.com/image.jpg

Tell me you're joking. Tell me they didn't make a download manager that can't handle SSL...

Better yet, tell me how to turn on SSL access in the Android download manager.

Mesenchyme answered 12/11, 2011 at 17:25 Comment(0)
A
7

I had the same problem previously. Yup I see HTTPS support is already in ICS, but not in 2.3.7 and below, but we can extract the source code to create a DownloadManager to support that.

Based on the sample code from http://android-er.blogspot.com/2011/07/sample-code-using-androidappdownloadman.html i made a demo with extracted DownloadManager to support HTTPS.

You can find the sample code here https://github.com/alvinsj/android-https-downloadmanager-demo, run by just changing the url to your https based url.

Anallise answered 10/5, 2012 at 12:21 Comment(1)
could you give more info about the HTTPS support in ICS? I may need to get the source code and replace the DownloadManager in 2.3.xZeldazelde
R
1

Yes it seems that DownloadManager only supports HTTP protocol: http://www.google.com/codesearch#cZwlSNS7aEw/frameworks/base/core/java/android/app/DownloadManager.java&exact_package=android&q=Can%20only%20download%20HTTP%20URIs&type=cs&l=343

I'm disappointed too as I just wanted to use it on a HTTPS site.

Reflectance answered 12/11, 2011 at 17:38 Comment(2)
FYI, the current online source code shows that it now supports HTTPS. I have not run any experiments to see when HTTPS support was added.Musaceous
I think that you got a broken LinkLovelace
L
-7

I've found a very easy solution for this:

request = new DownloadManager.Request(sourceUrl.replace("https://", "http://"))

Surprisingly worked for all https URLs that I tried. I'm not sure about the https security, but there is no exception and file gets downloaded properly.

Lyublin answered 28/6, 2012 at 9:5 Comment(4)
Not only does this response assume that the server is providing the content on both port 443 and port 80 (which is most likely not the case), it does not succeed in sending the data over a secure connection, which is the whole point of SSL. It does not "work"---the code merely obscures the fact that you're really downloading from a distinct, non-secure port. It's like if I want to go to Dover in the United Kingdom but my car can't make it out of Colorado, USA, so you do a destination.replace("o", "en") and wow, I'm at my destination! But now I'm really in Denver, not Dover...Mesenchyme
@GarretWilson Not actually... I've already tested what u've described here. DownloadManager is capable of downloading secure content from https sites. The only problem is the bug in DownloadManager.Request(url) constructor. It does a wrong validation. So if you specify an http url, it internally forwards it to https. I've tested it using my own server where we allow only secure connections. This can also be verified by opening an https url in browser using http - it will be automatically forwared to https site.Lyublin
If your browser redirects from HTTP to HTTPS it's only because your server is configured to listen on port 80 and issue a redirect to port 443. Perhaps you're saying that internally DownloadManager knows how to follow HTTP redirects and will correctly connect to an SSL sites if it's redirected to one. I haven't tested that. It's good to know for specific situations in which you control the server, but there's no guarantee all (or even most) servers will be so configured, so it's not a general solution.Mesenchyme
if your content is provided via http and https - good for you, in this case. but this can not be considered a workaround...Henson

© 2022 - 2024 — McMap. All rights reserved.