How can I use Android to download a session / cookie based file using webView?

I am trying to upload a file using webView from file nodes (e.g. zippyshare.com). The problem is that I cannot use intentions to open the browser or redirect it through the DownloadManager, since it is based on the session / cookie and runs these methods, it redirects the zip file to the original html file for reuse.

I 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 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 and save the cookie, and although I see that the headers are sent just fine, it still redirects to the html link, which leads me to believe that this is based on the session.

Is there a way to upload a file this way?

+6
source share
1 answer

I was dealing with the same problem and I managed to get your first solution to work, with only minor changes. Just replace the Set-Cookie width Cookie :

 request.addRequestHeader("Cookie", cookie); 

Btw. session-based means that auth data is not stored in cookies, but on the server side, identified by the key that is stored in cookies. Therefore, it doesn’t really matter if it works based on the session or not, cookies are used in both cases.

I also tried the second solution (it’s easier), but from what I read, it seems that Browser.EXTRA_HEADERS is only supported by Android browser by default. Therefore, if the user has another browser in his device, he will not work.

This is an old question, but I hope this helps someone.

+6
source

Source: https://habr.com/ru/post/950562/


All Articles