2011-11-15 100 views
1

時(特別是當我有一個不好的連接)時,我獲得NonRepeatableRequestException而 發送映像到服務器。這裏是我的代碼:NonRepeatableRequestException在發送鏡像到服務器

HttpParams params = new BasicHttpParams(); 

SchemeRegistry registry = new SchemeRegistry(); 
     registry.register(new Scheme("http", 
PlainSocketFactory.getSocketFactory(), 80)); 

DefaultHttpClient httpclient = new DefaultHttpClient(new 
ThreadSafeClientConnManager(params, registry), params); 

HttpPost httpPost = new HttpPost(uri); 
SimpleMultipartEntity multipartContent = new SimpleMultipartEntity(); 
multipartContent.addPart(propName, filename, inputStream, 
contentType); 
httpPost.setEntity(multipartContent); 
httpclient.getParams().setParameter("http.protocol.expect-continue", 
false); 

java.util.logging.Logger.getLogger("httpclient.wire.header").setLevel(java.util.logging.Level.FINEST); 
java.util.logging.Logger.getLogger("httpclient.wire.content").setLevel(java.util.logging.Level.FINEST); 
HttpResponse response = httpclient.execute(httpPost); 

我意識到的InputStream是不可重複的類型,但不知道如何正確地創建請求,並避免此異常。 任何幫助,不勝感激。

回答

0

發現,我可以添加自己的DefaultHttpRequestRetryHandler我的HttpClient:

httpclient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, false) { 

    public boolean retryRequest(IOException exception, int executionCount, 
      HttpContext context) { 
      if (executionCount > this.getRetryCount()) { 
       return false; 
      } 
      if (exception != null) { 
       return true; 
      } else { 
       return super.retryRequest(exception, executionCount, context); 
      } 

    } 
}); 
0

我發現這種情況的原因除了一個待提高。

根據this group可能會引發此異常,因爲服務器正在返回HTTP 401(未授權)。