Tôi đang cố tải xuống tệp bằng webView từ máy chủ tệp (như zippyshare.com). Vấn đề là, tôi không thể sử dụng ý định để mở một trình duyệt, hoặc định tuyến lại nó thông qua DownloadManager, vì nó dựa trên phiên/cookie và khởi chạy các phương thức này chuyển hướng tệp zip vào tệp html gốc để gỡ xuống.Tôi làm cách nào để sử dụng Android để tải xuống tệp dựa trên phiên/cookie bằng cách sử dụng webView?
Tôi đã thử:
Uri source = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(source);
String cookie = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("Set-Cookie", cookie);
request.addRequestHeader("User-Agent", view.getSettings().getUserAgentString());
request.addRequestHeader("Accept", "text/html, application/xhtml+xml, *" + "/" + "*");
request.addRequestHeader("Accept-Language", "en-US,en;q=0.7,he;q=0.3");
request.addRequestHeader("Referer", url);
// Use the same file name for the destination
final File destinationDir = new File (Environment.getExternalStorageDirectory(), cordova.getActivity().getPackageName());
if (!destinationDir.exists()) {
destinationDir.mkdir(); // Don't forget to make the directory if it's not there
}
File destinationFile = new File (destinationDir, source.getLastPathSegment());
Log.e("FILEPOSITION", Uri.fromFile(destinationFile).toString());
request.setDestinationUri(Uri.fromFile(destinationFile));
// Add it to the manager
manager.enqueue(request);
và:
Bundle bundle = new Bundle();
String cookie = CookieManager.getInstance().getCookie(url);
bundle.putString("cookie", cookie);
bundle.putString("User-Agent", view.getSettings().getUserAgentString());
Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(url));
intent.putExtra(Browser.EXTRA_HEADERS, bundle);
cordova.getActivity().startActivity(intent);
để cố gắng duy trì các tập tin cookie, và trong khi tôi thấy các tiêu đề được gửi tốt, nó vẫn chuyển hướng đến liên kết html , dẫn tôi tin rằng đó là phiên dựa trên.
Có cách nào để tải xuống tệp theo cách đó không?
Thanks tobik, tôi sẽ có một cái nhìn vào buổi tối, hy vọng nó hoạt động :) – trueicecold