0

I am using webview for showing my website to my users. The user needs to login to access files from my websites. Most of the files are in pdf format. But I noticed that the files not downloading properly and the files downloaded are just in html format and it only contain a error 44 error Page not found. After I googled about this problem I found that it may happening because the app doesn't in login state while downloading. I am new to android development so I request for a clear answer if possible for my understanding. Please check my code

webView.setDownloadListener(new DownloadListener() {


            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength){

                checkDownloadPermission();

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

                request.setDescription("Download file...");
                request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype));
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
                try {
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, new URL(url).getFile());
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                dm.enqueue(request);
                Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
            }
        });

    // This will handle downloading. It requires Gingerbread, though
        final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

        // This is where downloaded files will be written, using the package name isn't required
        // but it's a good way to communicate who owns the directory
        final File destinationDir = new File (Environment.getExternalStorageDirectory(), getPackageName());

        if (!destinationDir.exists()) {
            destinationDir.mkdir(); // Don't forget to make the directory if it's not there
        }
@Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {


                webView.getSettings().setDomStorageEnabled(true);
                CookieManager.getInstance().acceptCookie();
                CookieManager.setAcceptFileSchemeCookies(true);
                webView.getSettings().setJavaScriptEnabled(true);

                boolean shouldOverride = false;
                // We only want to handle requests for mp3 files, everything else the webview
                // can handle normally
                if (url.endsWith(".pdf")) {
                    shouldOverride = true;
                    Uri source = Uri.parse(url);



                    // Make a new request pointing to the mp3 url
                    DownloadManager.Request request = new DownloadManager.Request(source);

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

                    request.addRequestHeader("cookie", cookies);


                    // Use the same file name for the destination
                    File destinationFile = new File (destinationDir, source.getLastPathSegment());
                    request.setDestinationUri(Uri.fromFile(destinationFile));
                    // Add it to the manager
                    manager.enqueue(request);
                }
                else
                    view.loadUrl(url);
                return shouldOverride;
            }
  • 404 page not found or refused. Your credentials to get the pdf file were insufficient. So the server sent a 404 html file instead. Try to find out how to login correctly. – blackapps Mar 31 '22 at 15:30
  • @blackapps user login was successful but during the download the app is not login state. That's why the downloaded file is just html file and it shows 404 error. How can i resolve this issue? Help me if you can – Dony Peter Puthumana Apr 01 '22 at 08:47
  • I dont know how you can send the right credentials. I only explained you that you got a 404 html page instead of an expected pdf file. You had not told us yourself that you realized that it was that that happened. – blackapps Apr 01 '22 at 09:26
  • @blackapps how can i maintain login state while downloading contents? – Dony Peter Puthumana Apr 01 '22 at 09:32
  • Repeat: `I dont know how you can send the right credentials.` – blackapps Apr 01 '22 at 09:38

0 Answers0