我似乎遇到了一个特殊的问题,在Android 1.5,当我使用的库(signpost 1.1-SNAPSHOT),使两个连接到远程服务器。第二个连接总是失败,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 – 只是特定的网址,如上面的一个。
此外,如果我切换到使用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到似乎是其他地方类似的问题,但到目前为止没有解决方案。如果他们真的是同样的问题,那么问题可能不是用路标,因为其他引用没有参考。
有任何想法吗?