0

I am new to android development and I am trying to make a webapp for my website. I am stuck at downloading files. The user needs to be log in for accessing some pdf files. How can I download files from a website which is accessible only to authorised users?

I am using this code for download manager.

 webView.setDownloadListener(new DownloadListener()
   {

  @Override  


   public void onDownloadStart(String url, String userAgent,
        String contentDisposition, String mimeType,
        long contentLength) {

    DownloadManager.Request request = new DownloadManager.Request(
            Uri.parse(url));


    request.setMimeType(mimeType);


    String cookies = CookieManager.getInstance().getCookie(url);


    request.addRequestHeader("cookie", cookies);


    request.addRequestHeader("User-Agent", userAgent);


    request.setDescription("Downloading file...");


    request.setTitle(URLUtil.guessFileName(url, contentDisposition,
            mimeType));


    request.allowScanningByMediaScanner();


    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalPublicDir(
            Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
                    url, contentDisposition, mimeType));
    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    dm.enqueue(request);
    Toast.makeText(getApplicationContext(), "Downloading File",
            Toast.LENGTH_LONG).show();
}});
  • `String cookies = ...` Did you ever log or toast that string to see whats in it? You never explained why you would have cookies. Did you ever use hasCookies() ? – blackapps Apr 11 '22 at 07:58
  • `URLUtil.guessFileName(url, contentDisposition, mimeType)` You know what has been said. – blackapps Apr 11 '22 at 08:02

0 Answers0