如何使用Android来使用webView下载基于会话/ cookie的文件? [英] How can I use Android to download a session/cookie based file using webView?

查看:377
本文介绍了如何使用Android来使用webView下载基于会话/ cookie的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文件主机(如zippyshare.com)中使用webView下载文件。
问题是,我不能使用意图打开浏览器,或者通过DownloadManager重新路由它,因为它是基于会话/ cookie的,并且启动这些方法将zip文件重定向到原始的html文件以重新下载。 / p>

我尝试过:

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

//为目标使用相同的文件名
final File destinationDir = new File(Environment.getExternalStorageDirectory(),cordova.getActivity()。getPackageName());

if(!destinationDir.exists()){
destinationDir.mkdir(); //不要忘记使目录不存在
}

文件destinationFile = new File(destinationDir,source.getLastPathSegment());
Log.e(FILEPOSITION,Uri.fromFile(destinationFile).toString());
request.setDestinationUri(Uri.fromFile(destinationFile));
//将其添加到经理
manager.enqueue(request);

和:

 code> 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链接,这让我相信它是基于会话的。



有没有一种以这种方式下载文件的方式?

解决方案

我正在处理同样的问题,我设法使您的第一个解决方案工作,只有轻微的变化。只需替换 Set-Cookie width Cookie

  request.addRequestHeader(Cookie,cookie); 

Btw。基于会话的表示认证数据不存储在Cookie中,而是存储在服务器端,由密钥标识,存储在Cookie中。所以实际上并不重要,无论是基于会话还是不使用cookie。



我也尝试过第二个解决方案(更简单)我已经看过,似乎只有默认的Android浏览器才支持 Browser.EXTRA_HEADERS 。所以如果用户在他的设备上有不同的浏览器,它将无法工作。



这是一个旧问题,但我希望它能帮助某人。


I'm trying to download a file using webView from file hosts (like zippyshare.com). Problem is, I can't use intents to open a browser, or reroute it through DownloadManager, since it's session/cookie based, and launching those methods redirects the zip file into the original html file to re-downlad.

I've tried:

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

and:

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

to try to preserve the cookie, and while I see the headers are sent just fine, it still redirects to the html link, which leads me to believe it's session based.

Is there a way of downloading a file in that manner?

解决方案

I was dealing with the same problem and I managed to get your first solution working, only with a slight change. Just replace Set-Cookie width Cookie:

request.addRequestHeader("Cookie", cookie);

Btw. session based means that the auth data are not stored in cookies but on server side, identified by a key, which IS stored in cookies. So it actually doesn't matter whether it's session based or not, cookies are used in both cases.

I've also tried the second solution (it's simpler) but from what I've read it seems that Browser.EXTRA_HEADERS is supported only by the default Android browser. So if the user has a different browser in his device it won't work.

This is an old question but I hope it will help someone.

这篇关于如何使用Android来使用webView下载基于会话/ cookie的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆