2010-06-10 24 views
6

Önceden kimlik doğrulamasıyla çok fazla sorun yaşadıktan sonra sonunda çalıştım. Şimdi sıradaki sorun. Onunla bir dosya almak istiyorum ama nasıl olduğunu bilmiyorum. Dosya verilerinin değişken yanıtta olabileceğini düşündüm, ama değil. Bunun nasıl olabileceği hakkında bir fikrin var mı? Ben :(başarılı olamadı günlerinden beri bunu çalışıyorum -.. Temelde ben Prem auth ile korunan bir sunucu üzerinde olan bir jpeg dosyası, indirmeye çalışıyorumDefaultHTTPClient ve preemptive authentication ile bir dosya indirme

 // 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

indirilecek resmin url ile sunucu cevap mu – primpap

+0

Hayır, URL'yi doğrudan örn http://123.123.123.123/admin/snapshot.jpg ve açılan kullanıyorum? Bir tarayıcı görüntüyü görüntülüyor.Bir dosyanın içeriğini (http.url içinde tanımlanmış) nasıl alacağımı bilmem gerekiyor, ancak şimdilik yanıt değişkeninde sadece sunucunun gönderdiği başlıkları bulabilirim. 1.1 200 tamam ... ve benzeri) ... – Nils

cevap

3

Peki, şimdi kendim çözüldü - burada çözüm:

 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

Bu tamamen yanlıştır .... Bu size içerik verir, dosya değil ... – debracey

İlgili konular