2013-07-29 5 views
6

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

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

はクッキーを保存しようとする、と私はヘッダがうまく送信された参照しながら、それはまだHTMLリンクにリダイレクトそれは私がそれがセッションに基づいていると信じさせる。

このようにファイルをダウンロードする方法はありますか?

答えて

6

私は同じ問題を扱っていましたが、最初の解決策は少ししか変更されていませんでした。ところで

request.addRequestHeader("Cookie", cookie); 

:ちょうどSet-CookieCookieを交換してください。 セッションベースは、認証データがCookieに格納されず、Cookieに格納されているキーで識別されるサーバー側に格納されることを意味します。したがって、実際にセッションベースであるかどうかにかかわらず、Cookieはどちらの場合でも使用されます。

私は2番目の解決策を試しましたが(もっと簡単ですが)、私が読んだところでは、Browser.EXTRA_HEADERSはデフォルトのAndroidブラウザのみでサポートされているようです。したがって、ユーザーが自分のデバイスに別のブラウザを持っていれば、動作しません。

これは古い質問ですが、誰かを助けることを願っています。

+0

tobikさん、ありがとうございました。 – trueicecold

関連する問題