title: How to get 401 response without handling it using try/catch in android
body: <p>I am using <code>HttpUrlConnection</code> to make network requests from my android application. Everything works fine except one thing, 401. Whenever the server returns response with status code 401, my app throws <code>IOException</code> with a message stating, <code>"no authentication challenge found"</code>. After googling it, I haven't found a single solution, but only workaround (handling it using try/catch, assuming its a 401 response).</p>
  
  <p>here is the code snippet:</p>
  
  <pre><code>public Bundle request(String action, Bundle params, String cookie) throws FileNotFoundException, MalformedURLException, SocketTimeoutException,
          IOException {
  
      OutputStream os;
  
      String url = baseUrl + action;
      Log.d(TAG, url);
      HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
      conn.setConnectTimeout(30 * 1000);
      conn.setReadTimeout(30 * 1000);
      conn.setRequestProperty("User-Agent", System.getProperties().getProperty("http.agent") + "AndroidNative");
      conn.setRequestMethod("POST");
      conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
      conn.setRequestProperty("Connection", "Keep-Alive");
      conn.setDoOutput(true);
      conn.setDoInput(true);
      if (cookie != null) {
          conn.setRequestProperty("Cookie", cookie);
      }
  
      if (params != null) {
          os = conn.getOutputStream();
          os.write(("--" + boundary + endLine).getBytes());
          os.write((encodePostBody(params, boundary)).getBytes());
          os.write((endLine + "--" + boundary + endLine).getBytes());
          uploadFile(params, os);
          os.flush();
          os.close();
      }
  
      conn.connect();
  
      Bundle response = new Bundle();
      try {
          response.putInt("response_code", conn.getResponseCode());
          Log.d(TAG, conn.getResponseCode() + "");
          response.putString("json_response", read(conn.getInputStream()));
          List&lt;String&gt; responseCookie = conn.getHeaderFields().get("set-cookie");
          // Log.d(TAG, responseCookie.get(responseCookie.size() - 1));
          response.putString("cookie", responseCookie.get(responseCookie.size() - 1));
      } catch (SocketTimeoutException e) {
          throw new SocketTimeoutException(e.getLocalizedMessage());
      } catch (FileNotFoundException e) {
          throw new FileNotFoundException(e.getLocalizedMessage());
      } catch (IOException e) {
          e.printStackTrace();
          response.putInt("response_code", HttpURLConnection.HTTP_UNAUTHORIZED);
          response.putString("json_response", read(conn.getErrorStream()));
      }
  
      // debug
      Map&lt;String, List&lt;String&gt;&gt; map = conn.getHeaderFields();
      for (String key : map.keySet()) {
          List&lt;String&gt; values = map.get(key);
          for (String value : values) {
              Log.d(key, value);
          }
      }
  
      conn.disconnect();
  
      return response;
  }
  </code></pre>
  
  <p>I really want to know, why this exception is thrown? What does authentication challenge mean? How to provide authentication challenge? what change I have to make in my code to overcome this situation?</p>
  
  <p>Please enlighten me.. :)</p>