使用apache http下载文件

时间:2012-06-04 21:10:32

标签: java download apache-httpclient-4.x

我试图从一个页面下载一个zip文件,该页面需要用户名/密码才能访问(基于html表单的身份验证)。我正在使用apache http库。 早些时候我曾经做过非常相似的事情,该页面只需要密码来下载文件。 这是我的代码

     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();
            }

如果你打开代码中提供的url,你会发现表单有两个参数名称email和密码,我提供了formparams(上面代码中注释的值)。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试使用BasicAuthentication。

http://hc.apache.org/httpclient-3.x/authentication.html#Authentication_Schemes http://hc.apache.org/httpclient-3.x/authentication.html#Examples