使用DefaultHTTPClient和预认证下载文件

6

在我遇到了很多关于预先授权的问题之后,我最终让它正常工作了。 现在是下一个问题。我想获取一个文件,但我不知道该怎么做。 我认为文件数据可能在变量响应中,但实际上并不是这样。 有什么想法吗?我已经尝试了几天,但没有成功 :( - 基本上我正在尝试下载一个jpeg文件,这个文件位于一个受预先授权保护的服务器上。

        // BASIC AUTH
        /*
         * ====================================================================
         *
         *  Licensed to the Apache Software Foundation (ASF) under one or more
         *  contributor license agreements.  See the NOTICE file distributed with
         *  this work for additional information regarding copyright ownership.
         *  The ASF licenses this file to You under the Apache License, Version 2.0
         *  (the "License"); you may not use this file except in compliance with
         *  the License.  You may obtain a copy of the License at
         *
         *      http://www.apache.org/licenses/LICENSE-2.0
         *
         *  Unless required by applicable law or agreed to in writing, software
         *  distributed under the License is distributed on an "AS IS" BASIS,
         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         *  See the License for the specific language governing permissions and
         *  limitations under the License.
         * ====================================================================
         *
         * This software consists of voluntary contributions made by many
         * individuals on behalf of the Apache Software Foundation.  For more
         * information on the Apache Software Foundation, please see
         * <http://www.apache.org/>.
         */
        //http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java
        httpclient = new DefaultHttpClient();

        httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(host, port), 
                new UsernamePasswordCredentials(username, password));

        // Generate BASIC scheme object and stick it to the local 
        // execution context
        BasicHttpContext localcontext = new BasicHttpContext();

        BasicScheme basicAuth = new BasicScheme();
        localcontext.setAttribute("preemptive-auth", basicAuth);

        //first request interceptor
        httpclient.addRequestInterceptor(new PreemptiveAuth(), 0);

       HttpHost targetHost = new HttpHost(host, port, "http"); 

        //HttpGet httpget = new HttpGet("/");

        HttpGet httpget = new HttpGet(http.url); 

        System.out.println("executing request" + httpget.getRequestLine());
        /// !!!
        HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println("+"+response.getStatusLine()+"+");
...

服务器是否会响应要下载的图片的URL? - Primal Pappachan
不,我直接使用URL,例如http://123.123.123.123/admin/snapshot.jpg,并在浏览器中打开图像。我需要知道如何获取文件的内容(在http.url中定义),但目前我只能在响应变量中找到服务器发送的标头(HTTP 1.1 200 OK等等)... - Nils
1个回答

3

好的,现在我已经解决了 - 这里是解决方案:

        StringBuffer tmp = new StringBuffer();  // for content

        if (entity != null) {
            InputStream is = entity.getContent();

            int l = 0;
            byte[] t = new byte[1024];

            while ((l = is.read(t)) != -1) {
                for (int i=0; i<l ; i++){
                    tmp.append((char)t[i]);
                }
            }
        }

        return tmp.toString(); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接