Note that there are some explanatory texts on larger screens.

plurals
  1. POHttpURLConnection.getResponseCode() returns -1 on second invocation
    text
    copied!<p>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 <code>HttpURLConnection.getResponseCode()</code> of <code>-1</code></p> <p>Here's a testcase that exposes the problem:</p> <pre><code>// BROKEN public void testDefaultOAuthConsumerAndroidBug() throws Exception { for (int i = 0; i &lt; 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() &gt;= 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() &gt; 0); } } </code></pre> <p>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.</p> <p>Note that this doesn't happen for any url -- just specific urls such as the one above.</p> <p>Also, if I switch to using HttpClient instead of HttpURLConnection, everything works fine:</p> <pre><code>// WORKS public void testCommonsHttpOAuthConsumerAndroidBug() throws Exception { for (int i = 0; i &lt; 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() &gt;= 0 ) ; assertTrue( response.getStatusLine().getStatusCode() == 200); } } </code></pre> <p>I've found <a href="http://groups.google.com/group/android-developers/browse_thread/thread/bc5454ffc71f77dc" rel="noreferrer">references</a> 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.</p> <p>Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload