How to transfer cookies from WebViewClient to URLConnection, browser or other android file loading technique

We have a site with support for .net forms, which the user visits through the WebViewClient in our Android application. One of the features of the site is the ability to log in and download some PDF files, however you need to log in to download PDF files.

We are currently implementing shouldOverrideUrlLoading and loading pdf through the following code when the correct condition is met.

 URL u = new URL(url); URLConnection conn = u.openConnection(); int contentLength = conn.getContentLength(); DataInputStream stream = new DataInputStream(u.openStream()); byte[] buffer = new byte[contentLength]; stream.readFully(buffer); stream.close(); DataOutputStream fos = new DataOutputStream(new FileOutputStream("/sdcard/download/file.pdf")); fos.write(buffer); fos.flush(); fos.close(); 

From the IIS logs, it is obvious that IIS does not consider this login request and redirects it to the login page.

We need a way to upload a file with an auth file saved in a file upload request, but we don’t understand how to save a cookie.

Another viable solution for us is to save the auth cookie between WebViewClient and the Android browser. If we can do this, we simply open the PDF file using the default action in the browser.

Edit: it looks like I can configure the auth cookie manually through

 conn.setRequestProperty("Cookie", ""); 

Now I just need to figure out how to read auth cookie from WebViewClient

+6
java android cookies asp.net-mvc forms-authentication
source share
2 answers

Since you are using ASP.NET Forms authentication , you need to copy the forms auth cookie from WebView to URLConnection . Fortunately, this is pretty straight forward. This code is in the implementation of shouldOverrideUrlLoading

 string url = "http://site/generatePdfBehindFormsAuth"; // get an instance of a cookie manager since it has access to our auth cookie CookieManager cookieManager = CookieManager.getInstance(); // get the cookie string for the site. This looks something like ".ASPXAUTH=data" String auth = cookieManager.getCookie(url).toString(); URLConnection conn = (URLConnection)new URL(url).openConnection(); // Set the cookie string to be sent for download. In our case we're just copying the // entire cookie string from the previous connection, so all values stored in // cookies are persisted to this new connection. This includes the aspx auth // cookie, otherwise it would not be authenticated // when downloading the file. conn.setRequestProperty("Cookie", auth); conn.setDoOutput(true); conn.connect(); // get the filename from the servers response, its typical value is something like: // attachment; filename="GeneratedPDFFilename.pdf" String filename = conn.getHeaderField("Content-Disposition").split("\"")[1]; // by default, we'll store the pdf in the external storage directory String fileRoot = "/sdcard/"; // Complete the download FileOutputStream f = new FileOutputStream(new File(fileRoot, filename)); InputStream in = conn.getInputStream(); byte[] buffer = new byte[1024]; int len1 = 0; while ( (len1 = in.read(buffer)) > 0 ) { f.write(buffer,0, len1); } f.close(); in.close(); 

NOTE. . One thing to be aware of is that you must NOT call getContentLength on a URLConnection . After 4 hours of debugging, wirehark finally showed that if you call getContentLength , a cookie will be sent for a request that receives the length of the content, but a cookie will not be sent for subsequent requests, even in the same URLConnection instance . I may be naive and this is by design (the documentation does not indicate that it is by design), but I could not manually set a cookie for the subsequent request of the file by calling setRequestProperty after calling getContentLength . If I tried to do this, I would bring power closer.

+8
source share

Have you looked at the CookieSyncManager class? I believe that this is what is necessary to save cookies received from the server and reuse them.

http://developer.android.com/reference/android/webkit/CookieSyncManager.html

+1
source share

All Articles