Login to secure site from webview and pass HttpUrlConnection url to download - Error 403

In my application, I have a webview that implements a loader. The download listener uses the HttpUrlConnection to download the file with the given URL. I'm having trouble logging into some secure site, say dropbox.com (from my website), and then try downloading the file. I am getting 403 HTTP_FORBIDDEN error.

Is it because the session stored in the Webview is unknown to the HttpUrlConnection opened by the loader?

How do I share the same session (share sessionID? Cookie? Httpclient?) Between my webview and HttpUrlConnection?

thank!

+3


source to share


2 answers


DownloadListener is one of the most annoying interfaces in android (why can't they just stream you outside of me, it had to make a request to pass information to DonwloadListener). Anyway, yes, your HttpUrlConnection doesn't know anything about any cookies that are currently playing in the webview. You can use CookieManager

to get detailed information about cookies from WebView and then add them to your request based on HttpUrlConnection, something like this,



String cookie = CookieManager.getInstance().getCookie(url);
if (cookie != null) req.setRequestProperty("cookie", cookie);

      

+2


source


Is it because the session held by the Webview is unknown to the HttpUrlConnection opened by the downloadlistener?

      

Maybe.



Why don't you use the same one WebView

to download the file after login?

myWebView.loadUrl(downloadURL);

      

0


source







All Articles