Я реализовал DownloadListener
в WebView
. Я могу скачать файлы, когда запрос GET
Запрос. Но загрузка никогда не начинается, когда запрос POST
Запрос.
webview.setDownloadListener(this);
webview.setWebViewClient(new CustomWebViewClient());
DownloadListener (который никогда не вызывается)
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Log.d("webview_down","onDownloadStart");
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setMimeType(mimetype);
//------------------------COOKIE!!------------------------
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
//------------------------COOKIE!!------------------------
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);
}
shouldOverrideUrlLoading (я вижу вызываемый URL, но при загрузке он просто создает поврежденный файл PDF)
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("webview_override",url);
if(url.contains("/downloadstatement.htm")){
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String mimeType = mime.getMimeTypeFromExtension(extension);
request.setMimeType(mimeType);
//------------------------COOKIE!!------------------------
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.setDescription("Downloading file...");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "bs.pdf");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
return false;
}
view.loadUrl(url);
return true;
}