Spring for Android
  1. Spring for Android
  2. ANDROID-89

HttpUrlConnection throws IOException for a 401 response

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Complete
    • Affects Version/s: 1.0.0.RC1
    • Fix Version/s: 1.0.0.RELEASE
    • Component/s: rest-template
    • Labels:
      None
    • Environment:
      Identified on Android Gingerbread (2.3.x)

      Description

      This was identified while investigating ANDROID-80. The Commons Http Client and HttpComponents Client both correctly set a 401 status code, while HttpURLConnection throws an IOException:

      java.io.IOException: Received authentication challenge is null

      The logs from unit testing illustrate this very clearly.

      Commons Http Client:

      I/TestRunner(10439): started: testGetEntityNotAuthorized(org.springframework.web.client.RestTemplateIntegrationCommonsClientTests)
      W/System.err(10439): 2012-05-02 16:04:46.842::INFO: jetty-0.0
      W/System.err(10439): 2012-05-02 16:04:46.855::INFO: Started SocketConnector@0.0.0.0:8080
      I/TestRunner(10439): failed: testGetEntityNotAuthorized(org.springframework.web.client.RestTemplateIntegrationCommonsClientTests)
      I/TestRunner(10439): ----- begin exception -----
      I/TestRunner(10439):
      I/TestRunner(10439): org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
      I/TestRunner(10439): at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:76)
      I/TestRunner(10439): at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:504)
      I/TestRunner(10439): at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:461)
      I/TestRunner(10439): at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:419)
      I/TestRunner(10439): at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:239)
      I/TestRunner(10439): at org.springframework.web.client.AbstractRestTemplateIntegrationTests.testGetEntityNotAuthorized(AbstractRestTemplateIntegrationTests.java:128)
      I/TestRunner(10439): at java.lang.reflect.Method.invokeNative(Native Method)
      I/TestRunner(10439): at java.lang.reflect.Method.invoke(Method.java:507)
      I/TestRunner(10439): at junit.framework.TestCase.runTest(TestCase.java:154)
      I/TestRunner(10439): at junit.framework.TestCase.runBare(TestCase.java:127)
      I/TestRunner(10439): at junit.framework.TestResult$1.protect(TestResult.java:106)
      I/TestRunner(10439): at junit.framework.TestResult.runProtected(TestResult.java:124)
      I/TestRunner(10439): at junit.framework.TestResult.run(TestResult.java:109)
      I/TestRunner(10439): at junit.framework.TestCase.run(TestCase.java:118)
      I/TestRunner(10439): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
      I/TestRunner(10439): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
      I/TestRunner(10439): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
      I/TestRunner(10439): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
      I/TestRunner(10439): ----- end exception -----
      I/TestRunner(10439): finished: testGetEntityNotAuthorized(org.springframework.web.client.RestTemplateIntegrationCommonsClientTests)

      HttpComponents Http Client:

      I/TestRunner(10439): started: testGetEntityNotAuthorized(org.springframework.web.client.RestTemplateIntegrationHttpComponentsClientTests)
      W/System.err(10439): 2012-05-02 16:04:51.069::INFO: jetty-0.0
      W/System.err(10439): 2012-05-02 16:04:51.081::INFO: Started SocketConnector@0.0.0.0:8080
      D/dalvikvm(10439): GC_FOR_MALLOC freed 553K, 46% free 4493K/8263K, external 1625K/2137K, paused 53ms
      W/DefaultRequestDirector(10439): Authentication error: Unable to respond to any of these challenges: {}
      I/TestRunner(10439): failed: testGetEntityNotAuthorized(org.springframework.web.client.RestTemplateIntegrationHttpComponentsClientTests)
      I/TestRunner(10439): ----- begin exception -----
      I/TestRunner(10439):
      I/TestRunner(10439): org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
      I/TestRunner(10439): at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:76)
      I/TestRunner(10439): at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:504)
      I/TestRunner(10439): at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:461)
      I/TestRunner(10439): at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:419)
      I/TestRunner(10439): at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:239)
      I/TestRunner(10439): at org.springframework.web.client.AbstractRestTemplateIntegrationTests.testGetEntityNotAuthorized(AbstractRestTemplateIntegrationTests.java:128)
      I/TestRunner(10439): at java.lang.reflect.Method.invokeNative(Native Method)
      I/TestRunner(10439): at java.lang.reflect.Method.invoke(Method.java:507)
      I/TestRunner(10439): at junit.framework.TestCase.runTest(TestCase.java:154)
      I/TestRunner(10439): at junit.framework.TestCase.runBare(TestCase.java:127)
      I/TestRunner(10439): at junit.framework.TestResult$1.protect(TestResult.java:106)
      I/TestRunner(10439): at junit.framework.TestResult.runProtected(TestResult.java:124)
      I/TestRunner(10439): at junit.framework.TestResult.run(TestResult.java:109)
      I/TestRunner(10439): at junit.framework.TestCase.run(TestCase.java:118)
      I/TestRunner(10439): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
      I/TestRunner(10439): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
      I/TestRunner(10439): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
      I/TestRunner(10439): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
      I/TestRunner(10439): ----- end exception -----
      I/TestRunner(10439): finished: testGetEntityNotAuthorized(org.springframework.web.client.RestTemplateIntegrationHttpComponentsClientTests)

      Standard J2SE facilities (HttpURLConnection):

      I/TestRunner(10439): started: testGetEntityNotAuthorized(org.springframework.web.client.RestTemplateIntegrationSimpleClientTests)
      W/System.err(10439): 2012-05-02 16:04:56.329::INFO: jetty-0.0
      W/System.err(10439): 2012-05-02 16:04:56.343::INFO: Started SocketConnector@0.0.0.0:8080
      I/TestRunner(10439): failed: testGetEntityNotAuthorized(org.springframework.web.client.RestTemplateIntegrationSimpleClientTests)
      I/TestRunner(10439): ----- begin exception -----
      I/TestRunner(10439):
      I/TestRunner(10439): org.springframework.web.client.ResourceAccessException: I/O error: Received authentication challenge is null; nested exception is java.io.IOException: Received authentication challenge is null
      I/TestRunner(10439): at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:471)
      I/TestRunner(10439): at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:419)
      I/TestRunner(10439): at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:239)
      I/TestRunner(10439): at org.springframework.web.client.AbstractRestTemplateIntegrationTests.testGetEntityNotAuthorized(AbstractRestTemplateIntegrationTests.java:128)
      I/TestRunner(10439): at java.lang.reflect.Method.invokeNative(Native Method)
      I/TestRunner(10439): at java.lang.reflect.Method.invoke(Method.java:507)
      I/TestRunner(10439): at junit.framework.TestCase.runTest(TestCase.java:154)
      I/TestRunner(10439): at junit.framework.TestCase.runBare(TestCase.java:127)
      I/TestRunner(10439): at junit.framework.TestResult$1.protect(TestResult.java:106)
      I/TestRunner(10439): at junit.framework.TestResult.runProtected(TestResult.java:124)
      I/TestRunner(10439): at junit.framework.TestResult.run(TestResult.java:109)
      I/TestRunner(10439): at junit.framework.TestCase.run(TestCase.java:118)
      I/TestRunner(10439): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
      I/TestRunner(10439): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
      I/TestRunner(10439): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
      I/TestRunner(10439): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
      I/TestRunner(10439): Caused by: java.io.IOException: Received authentication challenge is null
      I/TestRunner(10439): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:1153)
      I/TestRunner(10439): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:1095)
      I/TestRunner(10439): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1048)
      I/TestRunner(10439): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:726)
      I/TestRunner(10439): at org.springframework.http.client.SimpleClientHttpResponse.getStatusCode(SimpleClientHttpResponse.java:49)
      I/TestRunner(10439): at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
      I/TestRunner(10439): at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:457)
      I/TestRunner(10439): ... 15 more
      I/TestRunner(10439): ----- end exception -----
      I/TestRunner(10439): finished: testGetEntityNotAuthorized(org.springframework.web.client.RestTemplateIntegrationSimpleClientTests)

        Activity

        Hide
        Roy Clarkson added a comment -

        Catching the exception in SimpleClientHttpResponse and returning HttpStatus.UNAUTHORIZED.

        Show
        Roy Clarkson added a comment - Catching the exception in SimpleClientHttpResponse and returning HttpStatus.UNAUTHORIZED.

          People

          • Assignee:
            Roy Clarkson
            Reporter:
            Roy Clarkson
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: