简体   繁体   English

使用apache http下载文件

[英]download a file using apache http

I was trying to download a zip file from a page which requries username/password to access ( html form based authentication). 我试图从需要用户名/密码的页面下载一个zip文件以进行访问(基于html表单的身份验证)。 I am using apache http library for it. 我正在使用apache http库。 Earlier I had worked on something very similar, that page required just password to download the file. 早些时候我从事过非常类似的工作,该页面只需要密码即可下载文件。 Here is my 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 you open the url provided in the code you will find the form has two parameters by the name email and password , and I have supplied them as formparams ( values commented in the code above ). 如果您打开代码中提供的url,您会发现该表单具有两个参数,名称分别为email和password,我已将它们作为formparams提供(上面代码中注释的值)。 Any help will be greatly appreciated. 任何帮助将不胜感激。

Try using BasicAuthentication. 尝试使用BasicAuthentication。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM