8

ファイル ホスト (zippyshare.com など) から webView を使用してファイルをダウンロードしようとしています。問題は、インテントを使用してブラウザーを開いたり、DownloadManager を介して再ルーティングしたりできないことです。これは、セッション/Cookie ベースであるためです。これらのメソッドを起動すると、zip ファイルが元の html ファイルにリダイレクトされて再ダウンロードされます。

私はもう試した:

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);

と:

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);

Cookie を保存しようとすると、ヘッダーが正常に送信されていることがわかりますが、それでも html リンクにリダイレクトされるため、セッション ベースであると思われます。

その方法でファイルをダウンロードする方法はありますか?

4

1 に答える 1

6

私は同じ問題を扱っていましたが、わずかな変更で最初のソリューションを機能させることができました。Set-Cookiewidthを置き換えるだけCookieです:

request.addRequestHeader("Cookie", cookie);

ところで。セッションベースとは、認証データが Cookie に保存されるのではなく、Cookie に保存されるキーによって識別されるサーバー側に保存されることを意味します。したがって、実際にはセッションベースかどうかは問題ではなく、どちらの場合でも Cookie が使用されます。

私も 2 番目の解決策を試しました (より簡単です) が、私が読んだBrowser.EXTRA_HEADERS限りでは、デフォルトの Android ブラウザーでのみサポートされているようです。そのため、ユーザーがデバイスに別のブラウザーを使用している場合、それは機能しません。

これは古い質問ですが、誰かの役に立てば幸いです。

于 2014-05-03T13:50:46.820 に答える