下载文件DefaultHTTPClient和preemptive认证文件、DefaultHTTPClient、preemptive

2023-09-06 02:52:10 作者:腾讯官方认证帅哥

在我有很多的问题,preemptive认证,我得到了它最后的工作。 现在,下一个问题。我想它的文件,但我不知道怎么办。 我认为该文件的数据可能会在不同反应,但事实并非如此。 任何想法如何这可能会奏效?我想它,因为没有成功天:( - 。基本上我试图下载一个JPEG文件,它是由$ P $点身份验证保护的服务器上

  //基本身份验证
        / *
         * ================================================= ===================
         *
         *一个或多个下授权给Apache软件基金会(ASF)
         * contributor许可证协议。看到的:通知文件
         *此工作的关于版权的所有权。
         *该asf文件许可证可以在apache许可下,2.0版
         *(以下简称许可证);你可能不使用这个文件除了在合规
         *许可证。您可以在获得许可证的副本
         *
         * http://www.apache.org/licenses/LICENSE-2.0
         *
         *除非适用法律要求或书面同意,软件
         *许可证下发布分布在原样的基础上,
         *无担保或任何形式的条件,无论是EX preSS或暗示的保证。
         *请参阅许可的特定语言的管理权限和
         *许可下限制。
         * ================================================= ===================
         *
         *此软件由许多自愿捐款
         *代表Apache软件基金会的人。欲了解更多
         *在Apache软件基金会的信息,请参阅
         *< HTTP://www.apache.org/> ;.
         * /
        //http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/Client$p$pemptiveBasicAuthentication.java
        HttpClient的=新DefaultHttpClient();

        httpclient.getCredentialsProvider()。setCredentials方法(
                新AuthScope(主机,端口),
                新UsernamePasswordCredentials(用户名,密码));

        //生成基本的计划对象,并将其粘到本地
        //执行上下文
        BasicHttpContext localcontext =新BasicHttpContext();

        BasicScheme BASICAUTH =新BasicScheme();
        localcontext.setAttribute(preemptive  - 身份验证,BASICAUTH);

        //第一个请求拦截器
        httpclient.addRequestInterceptor(新preemptiveAuth(),0);

       HttpHost targetHost =新HttpHost(主机,端口,HTTP);

        // HTTPGET HTTPGET =新HTTPGET(/);

        HTTPGET HTTPGET =新HTTPGET(http.url);

        的System.out.println(执行请求+ httpget.getRequestLine());
        ///!
        HTT presponse响应= httpclient.execute(targetHost,HTTPGET,localcontext);
        HttpEntity实体= response.getEntity();

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

解决方案

嗯,我解决了它自己了 - 这里的解决方案:

  StringBuffer的TMP =新的StringBuffer(); //内容

        如果(实体!= NULL){
            InputStream的是= entity.getContent();

            int类型l = 0;
            byte []的T =新的字节[1024];

            而((1- = is.​​read(吨))!=  -  1){
                的for(int i = 0; I<升;我++){
                    tmp.append((char)的T [i]);
                }
            }
        }

        返回tmp.toString();
 
Android HttpClient文件下载

After I had a lot of problems with preemptive authentication , I got it finally working. Now the next problem. I want to get a file with it, but I don't know how. I thought the file data might be in the variable response, but it isn't. Any ideas how this might work? I'm trying it since days without success :( - Basically I'm trying to download an jpeg file, which is on a server protected by 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()+"+");
...

解决方案

Well, I solved it myself now - here the solution:

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