2013-07-29 2 views
6

파일 호스트 (예 : zippyshare.com)에서 webView를 사용하여 파일을 다운로드하려고합니다. 문제는 세션/쿠키 기반이므로 브라우저를 열거 나 DownloadManager를 통해 경로를 변경하기 위해 인 텐트를 사용할 수 없다는 것입니다. 이러한 메서드를 실행하면 zip 파일이 원래 HTML 파일로 리디렉션되어 재 다운로드됩니다.Android를 사용하여 webView를 사용하여 세션/쿠키 기반 파일을 다운로드하려면 어떻게해야합니까?

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

나는 동일한 문제를 다루고 있었고 약간의 변화만으로도 첫 번째 해결책을 얻을 수있었습니다. Btw은

request.addRequestHeader("Cookie", cookie); 

: 그냥 Set-CookieCookie를 교체합니다. 세션 기반은 인증 데이터가 쿠키에 저장되지 않지만 쿠키에 저장되는 키로 식별되는 서버 측에 있음을 의미합니다. 따라서 실제로 세션 기반인지 여부에 관계없이 두 가지 경우 모두 쿠키가 사용됩니다.

두 번째 솔루션을 사용해 보았습니다. (더 간단합니다)하지만 읽은 바에 따르면 기본 안드로이드 브라우저에서만 Browser.EXTRA_HEADERS이 지원됩니다. 따라서 사용자가 장치에 다른 브라우저를 사용하면 작동하지 않습니다.

이것은 오래된 질문이지만 도움이되기를 바랍니다.

+0

감사합니다 tobik, 나는 저녁에 한 번 봐야 겠어, 그것이 효과가 있길 바래. :) – trueicecold