2010-06-10 55 views
6

當我在搶先認證方面遇到很多問題後,我終於開始工作了。 現在是下一個問題。我想用它得到一個文件,但我不知道如何。 我認爲文件數據可能在變量響應中,但事實並非如此。 任何想法這可能工作?我想它,因爲沒有成功天:( - 。基本上我試圖下載一個JPEG文件,它是由炳身份驗證保護的服務器上使用DefaultHTTPClient下載文件並進行搶先認證

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

服務器是否響應與要下載的圖像的URL – primpap 2010-06-10 05:14:47

+0

不,我使用的是直接在URL如http://123.123.123.123/admin/snapshot.jpg並在打開?我需要知道如何獲取文件的內容(這在http.url中定義),但現在我只能在響應變量中找到服務器發送的頭(HTTP 1.1 200 OK ...等等)... – Nils 2010-06-10 14:50:22

回答

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

這是完全錯誤的....這給你的內容,而不是文件... – debracey 2011-08-22 18:53:20

相關問題