android - 使用 DefaultHTTPClient 和抢先身份验证下载文件

标签 android file authentication download preemptive

在我遇到很多抢先式身份验证问题之后,我终于让它工作了。 现在下一个问题。我想用它获取一个文件,但我不知道如何获取。 我认为文件数据可能在变量响应中,但事实并非如此。 任何想法这可能如何工作?我尝试了好几天都没有成功 :( - 基本上我正在尝试下载一个 jpeg 文件,该文件位于受 prem.auth 保护的服务器上。

        // 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()+"+");
...

最佳答案

好吧,我现在自己解决了 - 这里是解决方案:

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

关于android - 使用 DefaultHTTPClient 和抢先身份验证下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3011770/

相关文章:

android - 程序在每次执行时生成相同的 'random' 数字

file - Emacs Lisp 中是否有相当于 Python 的 __file__ 的东西

javascript - Angular 和 MongoDB,安全性?

java - 如何使用 LDAP Active Directory 验证 JSP 登录页面

http - 仅当协商失败时如何从协商方法回退到基本方法

android:windowSoftInputMode ="stateAlwaysHidden|adjustPan"不适用于所有设备

java - 在 Webview 中打开 .pdf 链接

java - PrintWriter 可以写入 "open"文本文件 (Java)

javascript - 无法在Leaflet map 上绘制从geoJson获取的点

bash scp 没有那个文件或目录