簡體   English   中英

使用apache http下載文件

[英]download a file using apache http

我試圖從需要用戶名/密碼的頁面下載一個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和password,我已將它們作為formparams提供(上面代碼中注釋的值)。 任何幫助將不勝感激。

嘗試使用BasicAuthentication。

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