2011-08-03 52 views
0

我沒有改變這種方法,但突然它需要很長時間。下面的代碼示例會產生此錯誤。Commons HttpClient getResponse需要很長的時間200秒

編輯:我提取的方法在一個單一的Java應用程序,並試圖加載文件,我有相同的超時。 3或4時間,他經過這個循環HttpMethodBase:691他停在我的本地PC上500秒,線程正在睡覺。睡覺下一行後outstream.close();

while ((len = instream.read(buffer)) > 0) { 
    outstream.write(buffer, 0, len); 
} 

編輯:這裏是示例代碼,如果你想嘗試在家裏:)(HttpClient的3.1)

import java.io.IOException; 

import org.apache.commons.httpclient.HttpClient; 
import org.apache.commons.httpclient.HttpStatus; 
import org.apache.commons.httpclient.methods.GetMethod; 


public class TestLoadImage { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     byte[] image = loadPhotoFromUrl("https://fbcdn-sphotos-a.akamaihd.net/photos-ak-snc1/v2635/232/115/68310606562/n68310606562_2255479_948765.jpg"); 
     System.out.println(image.length); 
    } 

    private static class GetMethodIgnoringContentLength extends GetMethod { 

     public GetMethodIgnoringContentLength(String uri) { 
      super(uri); 
     } 

     @Override 
     public long getResponseContentLength() { 
      // ignores content-length header, fakes "not specified": 
      return -1; 
     } 

    } 
    public static byte[] loadPhotoFromUrl(String photoUrl) { 
     HttpClient httpClient = new HttpClient(); 
     httpClient.getParams().setBooleanParameter("http.connection.stalecheck", true); 
     GetMethod get = new GetMethodIgnoringContentLength(photoUrl); 

     try { 
      int httpStatus = httpClient.executeMethod(get); 
      if (httpStatus == HttpStatus.SC_OK) { 
       byte[] imageBytes = get.getResponseBody(); 
       if (imageBytes.length > 0) { 
        return imageBytes; 
       } else { 
        System.out.println("Failed: empty response/zero data"); 
       } 
      } else { 
       System.out.println("Failed: "+ httpStatus); 
      } 
     } catch (IOException ioe) { 
      System.out.println(ioe); 
     } finally { 
      get.releaseConnection(); 
     } 

     return null; 
    } 
} 
+1

可能是緩慢的服務器或網絡連接不良。相同的URL在瀏覽器中加載速度還是加快? – Thilo

+0

jeah加載了一張臉譜資料圖片,在瀏覽器中即時顯示。圖像中顯示的所有「十六進制」數據顯示在日誌中後顯示「中斷」。我現在嘗試激活org.apache跟蹤日誌,正是他正在做的是破解。 – webstrap

+0

和過去一樣的服務器,以及其他對Facebook的請求都是正常的速度。 – webstrap

回答

0

與更新修復它HttpClient4 .1(以及爲此所需的一些重構)。但是如果有人知道上面例子中的原因,我切換正確的答案。