Tech Blog

Android

Common Things To Consider When Downloading Files in Custom Browsers Application For Android

By yash Pandya2 min read
← Back to blog

More often than not when developing a custom browser in android, we face many hurdles at the time of downloading the files. On this blog we will look into 2 major things that Developers need to consider while developing their browser app.

  1. Adding Cookies to Android’s DownloadManager requests.

When downloading a file from a website where users have to login to be able to access/download the file, we might face access denied error when processing the download. To fix this we need to add cookies from that particular url in the Download Requests.

If Android’s own Download manager is used to initiate download the below will the code : 

android.app.DownloadManager.Request mRequest;
…
String mStringCookieForUrl = “”;
mStringCookieForUrl = CookieManager.getInstance().getCookie(url);
mRequest.addRequestHeader(“cookie”,mStringCookieForUrl);
…

If HttpURLConnection is being used below code can be used : 

final HttpURLConnection mHttpURLConnection  = (HttpURLConnection) url.openConnection();…
String mStringCookieForUrl = “”;
mStringCookieForUrl = CookieManager.getInstance().getCookie(url);
mHttpURLConnection.addRequestProperty(“Cookie”,mStringCookieForUrl);…

In the code above in the download request, we need to add the cookie string of that specific url. To do this we have to use CookieManager.getInstance().getCookie(url) inbuilt method to retrieve the cookie string. 

If you don’t want to use CookieManager class, you can retrieve the cookie string using the following code too : 

String mStringCookieForUrl = “”;
mStringCookieForUrl = mWebView.getCookieForUrl(url);

2. Adding User-Agent at the time of download : 

When downloading a file, sometimes we face errors when downloading and in the output stream we get html content instead of the actual file. So in result, User will get a corrupted file. This issue happens because some websites are designed in such a way that it only supports only some types of Browsers like Chrome, FireFox etc.  So even if your browser is supported by the site via user agent, in the request header you need to specify the user-agent field to make it work

To do this we need to retrieve our user agent from the webview at the time of making the download request.

final HttpURLConnection mHttpURLConnection  = (HttpURLConnection) url.openConnection();…
String mUserAgent = mWebView.getSettings().getUserAgentString();mHttpURLConnection.addRequestProperty(“User-Agent”,mUserAgent);…

To add custom user agent, you can just add user agent string in the addRequestProperty method of HttpURLConnection object like below : 

mHttpURLConnection.addRequestProperty(“User-Agent”,<Custom-User-Agent>);

Keep reading

You may be interested in

Browse all articles →
Android Download file with Data URL
July 23, 20241 min read

Android Download file with Data URL

What is a Data URL? Data URLs are the type of URL scheme that is used to embed usually small types of files. In this URL, inline files are prefixed with the data: MIME type. data:[<mediatype>][;base64],<data> To download files having Data URLs inside Android: Firstly, we need to get an extension of the file from […]

By Bansari PanditRead More →
Download the file with blob URL in Android WebView
July 23, 20242 min read

Download the file with blob URL in Android WebView

What is the Blob URL? Blob URLs can only be generated by the browser with use of internal mapping and used within the current document while it’s open which means it is valid for the same session (ie. the life of the page/document). The blob URL looks like this: blob:http://example.com/550e8400-e29b-41d4-a716-446655440000 Below is the sample HTML […]

By Bansari PanditRead More →
Key Terminologies in Android Enterprise
December 20, 20227 min read

Key Terminologies in Android Enterprise

Android Enterprise is a Google-led initiative to enable the use of Android devices and applications in a workspace. This program offers APIs and tools for developers to integrate support of Android devices into the EMM (enterprise mobility management) or UEM (Unified Endpoint Management) solutions. The following are the key terms associated with Android Enterprise. This […]

By Sandeep KadamRead More →