java - download a file using apache http -


i trying download zip file page requries username/password access ( html form based authentication). using apache http library it. earlier had worked on similar, page required password download file. here code

     defaulthttpclient httpclient = new defaulthttpclient();             httpclient.setredirectstrategy(new defaultredirectstrategy() {                         public uri lastredirecteduri;                          public boolean isredirected(httprequest request,                                                      httpresponse response,                                                      httpcontext context) {                             boolean isredirect = false;                             try {                                 isredirect =                                          super.isredirected(request, response,                                                             context);                             } catch (org.apache.http.protocolexception e) {                                 e.printstacktrace();                             }                             if (!isredirect) {                                 int responsecode =                                      response.getstatusline().getstatuscode();                                 if (responsecode == 301 ||                                      responsecode == 302) {                                     system.out.println("the original response code is" + responsecode);                                     return true;                                 }                             }                             return isredirect;                         }  //                        public uri getlocationuri(httpresponse response, httpcontext context) //                                    throws protocolexception { // //                                lastredirecteduri = super.getlocationuri(request , response, context); // //                                return lastredirecteduri; //                            }                      });               list<namevaluepair> formparams = new arraylist<namevaluepair>();            // formparams.add(new basicnamevaluepair("password", arg[1]));             formparams.add(new basicnamevaluepair("password", "*****"));             formparams.add(new basicnamevaluepair("email", "****"));             urlencodedformentity entity1 =                  new urlencodedformentity(formparams, "utf-8");             httppost httppost =                  new httppost("https://*************************/l/?next=/s/48750/d/");                // new httppost(arg[0]);             httppost.setentity(entity1);               httpcontext localcontext = new basichttpcontext();             cookiestore cookiestore = new basiccookiestore();             localcontext.setattribute(clientcontextconfigurer.cookie_store, cookiestore);             httpresponse response = httpclient.execute(httppost, localcontext);             httphost target =                  (httphost)localcontext.getattribute(executioncontext.http_target_host);             system.out.println("final target: " + target);               system.out.println(response.getprotocolversion());             system.out.println(response.getstatusline().getstatuscode());             system.out.println(response.getstatusline().getreasonphrase());             system.out.println(response.getstatusline().tostring());              httpentity entity = response.getentity();                         if (entity != null) {                             fileoutputstream fos =                                  new java.io.fileoutputstream("download.zip");                             entity.writeto(fos);                             fos.close();             } 

if open url provided in code find form has 2 parameters name email , password , , have supplied them formparams ( values commented in code above ). appreciated.

try using basicauthentication.

http://hc.apache.org/httpclient-3.x/authentication.html#authentication_schemes http://hc.apache.org/httpclient-3.x/authentication.html#examples


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -