HttpURLConnection.getResponseCode() 在第二次调用时返回 -1
当我使用的库(路标 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,例如上面的 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);
}
}
我发现 引用其他地方似乎存在类似问题,但到目前为止还没有解决方案。如果它们确实是同一个问题,那么问题可能不在于路标,因为其他参考文献没有提及它。
有什么想法吗?
I seem to be running into a peculiar problem on Android 1.5 when a library I'm using (signpost 1.1-SNAPSHOT), makes two consecutive connections to a remote server. The second connection always fails with a HttpURLConnection.getResponseCode()
of -1
Here's a testcase that exposes the problem:
// 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);
}
}
Basically, if I sign the request and then consume the entire input stream, the next request will fail with a resultcode of -1. The failure doesn't seem to happen if I just read one character from the input stream.
Note that this doesn't happen for any url -- just specific urls such as the one above.
Also, if I switch to using HttpClient instead of HttpURLConnection, everything works fine:
// 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);
}
}
I've found references to what seems to be a similar problem elsewhere, but so far no solutions. If they're truly the same problem, then the problem probably isn't with signpost since the other references make no reference to it.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试设置此属性以查看是否有帮助,
当 UrlConnection 无法理解服务器响应并且客户端/服务器不同步时,我看到了类似的问题。
如果这解决了您的问题,您必须获取 HTTP 跟踪才能准确了解响应的特殊之处。
编辑:这一变化证实了我的怀疑。它不能解决你的问题。它只是隐藏了症状。
如果第一个请求的响应是 200,我们需要跟踪。我通常使用 Ethereal/Wireshark 来获取 TCP 跟踪。
如果您的第一个响应不是 200,我确实发现您的代码存在问题。使用 OAuth,错误响应 (401) 实际上会返回数据,其中包括 ProblemAdvice、Signature Base String 等以帮助您调试。您需要从错误流中读取所有内容。否则,它将混淆下一个连接,这就是 -1 的原因。以下示例向您展示如何正确处理错误,
Try set this property to see if it helps,
I saw similar problems when server response is not understood by UrlConnection and client/server gets out of sync.
If this solves your problem, you have to get a HTTP trace to see exactly what's special about the response.
EDIT: This change just confirms my suspicion. It doesn't solve your problem. It just hides the symptom.
If the response from first request is 200, we need a trace. I normally use Ethereal/Wireshark to get the TCP trace.
If your first response is not 200, I do see a problem in your code. With OAuth, the error response (401) actually returns data, which includes ProblemAdvice, Signature Base String etc to help you debug. You need to read everything from error stream. Otherwise, it's going to confuse next connection and that's the cause of -1. Following example shows you how to handle errors correctly,
当我在关闭输入流并打开第二个连接之前没有从输入流中读取所有数据时,我遇到了同样的问题。它也可以通过 System.setProperty("http.keepAlive", "false"); 修复,或者只是循环,直到我读完 InputStream 的其余部分。
与您的问题不完全相关,但希望这对遇到类似问题的其他人有所帮助。
I've encountered the same problem when I did not read in all the data from the InputStream before closing it and opening a second connection. It was also fixed either with
System.setProperty("http.keepAlive", "false");
or simply just looping until I've read the rest of the InputStream.Not completely related to your issue, but hope this helps anyone else with a similar problem.
Google 提供了一个优雅的解决方法,因为它只在 Froyo 之前发生:
Cf。 http://android-developers.blogspot.ca/2011/09 /androids-http-clients.html
Google provided an elegant workaround since it's only happening prior to Froyo:
Cf. http://android-developers.blogspot.ca/2011/09/androids-http-clients.html
或者,您可以在连接 (HttpUrlConnection) 中设置 HTTP 标头:
Or, you can set HTTP header in the connection (HttpUrlConnection):
在阅读完响应之前,您能否验证连接是否未关闭?也许 HttpClient 会立即解析响应代码,并将其保存以供将来查询,但是一旦连接关闭,HttpURLConnection 可能会返回 -1 ?
Can you verify that the connection is not getting closed before you finish reading the response? Maybe HttpClient parses the response code right away, and saves it for future queries, however HttpURLConnection could be returning -1 once the connection is closed?