当我正在使用的库(signpost 1.1-SNAPSHOT)与远程服务器建立两个连续连接时,我似乎在 Android 1.5 上遇到了一个特殊问题。第二个连接总是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。如果我只从输入流中读取一个字符,则似乎不会发生故障。
请注意,任何 url 都不会发生这种情况——只是特定的 url,例如上面的那个。
此外,如果我切换到使用 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);
}
}
我在其他地方找到了对似乎类似问题的参考,但到目前为止还没有解决方案。如果它们确实是同一个问题,那么问题可能不在于路标,因为其他参考文献没有提及它。
有任何想法吗?