當我使用的庫(路標1.1-SNAPSHOT)與一個遠程服務器建立兩個連續連接時,我似乎在Android 1.5上遇到了一個特殊問題。第二個連接始終失敗了HttpURLConnection.getResponseCode()
的-1
HttpURLConnection.getResponseCode()在第二次調用時返回-1
下面是暴露問題的測試用例:
// BROKEN
public void testDefaultOAuthConsumerAndroidBug() throws Exception {
for (int i = 0; i < 2; ++i) {
final HttpURLConnection c = (HttpURLConnection) new URL("https://api.tripit.com/oauth/request_token").openConnection();
final DefaultOAuthConsumer consumer = new DefaultOAuthConsumer(api_key, api_secret, SignatureMethod.HMAC_SHA1);
consumer.sign(c); // This line...
final InputStream is = c.getInputStream();
while(is.read() >= 0) ; // ... in combination with this line causes responseCode -1 for i==1 when using api.tripit.com but not mail.google.com
assertTrue(c.getResponseCode() > 0);
}
}
基本上,如果我簽名的請求,然後消耗整個輸入流,下一個請求將失敗結果碼爲-1。如果我只是從輸入流中讀取一個字符,失敗似乎不會發生。
請注意,這不會發生任何網址 - 只是具體的網址,如上面的一個。
另外,如果我切換到使用HttpClient的,而不是HttpURLConnection的,一切工作正常:
// WORKS
public void testCommonsHttpOAuthConsumerAndroidBug() throws Exception {
for (int i = 0; i < 2; ++i) {
final HttpGet c = new HttpGet("https://api.tripit.com/oauth/request_token");
final CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(api_key, api_secret, SignatureMethod.HMAC_SHA1);
consumer.sign(c);
final HttpResponse response = new DefaultHttpClient().execute(c);
final InputStream is = response.getEntity().getContent();
while(is.read() >= 0) ;
assertTrue(response.getStatusLine().getStatusCode() == 200);
}
}
我發現references什麼似乎是一個類似的問題在其他地方,但至今沒有解決方案。如果他們真的是同一個問題,那麼問題可能不是路標,因爲其他參考文獻沒有提及它。
任何想法?
有趣。在測試用例的開頭添加'System.setProperty(「http.keepAlive」,「false」)'完全解決了這個問題。 有關如何執行http跟蹤的任何建議?我是否需要使用日誌記錄代理,或者我可以直接在客戶端上做些什麼? – emmby 2009-09-17 22:10:59
查看我的編輯.............. – 2009-09-17 23:25:57
確認它是一個android bug,我們在這裏跟蹤它: http://code.google.com/p/android/issues/詳細信息?id = 7786 – 2010-05-13 21:03:00