Java中getresponse_java – HttpURLConnection.getResponseCode()在第二次调用时返回-1

我似乎遇到了一个特殊的问题,在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到似乎是其他地方类似的问题,但到目前为止没有解决方案。如果他们真的是同样的问题,那么问题可能不是用路标,因为其他引用没有参考。

有任何想法吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值