java - Android: Download file from password protected website -


i need download pdf files site users have login before can download files.

if user not logged site, links pdf files redirect login page.

i managed post user credentials login page , save cookie response , tried providing cookie urlconnection when attempting download, still getting login page instead of pdf file.

update: code added.

posting user credentials:

httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setrequestmethod("post");  outputstream os = conn.getoutputstream(); bufferedwriter writer = new bufferedwriter(new outputstreamwriter(os, "utf-8")); writer.write("username=" + username + "&password=" + password); writer.flush(); writer.close(); os.close();  string result = httputils.getresponsestring(conn); if (result.indexof("invalid") == -1) {     string cookie = conn.getheaderfield("set-cookie");     return cookie; } 

cookie stored , used in following code segment

attempting download pdf :

urlconnection conn = url.openconnection(); conn.setrequestproperty("cookie", _cookie); conn.connect(); inputstream instream = new bufferedinputstream(url.openstream(), 1024); outputstream outstream = new fileoutputstream(_pdffile); byte buffer[] = new byte[1024]; int count = 0; while ((count = instream.read(buffer)) != -1)     outstream.write(buffer, 0, count); outstream.flush(); outstream.close(); instream.close(); 


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -