title: Android, HttpURLConnection and handling bad credentials
body: <p>I'm performing a GET request:</p>
  
  <pre><code>HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
  urlConnection.setConnectTimeout(CONNECT_TIMEOUT);
  urlConnection.setReadTimeout(READ_TIMEOUT);
  urlConnection.connect();
  </code></pre>
  
  <p>by which time the credentials have already been set with:</p>
  
  <pre><code>Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
       return new PasswordAuthentication(loginNameString, passwordString.toCharArray());
    }
  });
  </code></pre>
  
  <p>Everything works fine when I supply good credentials (i.e. username and password).</p>
  
  <p>What happens to the connection when bad credentials are supplied? For example, if I deliberately supply bad credentials and I call <code>urlConnection.getResponseCode()</code> immediately after <code>urlConnection.connect()</code> my app eventually times out and I have to force close. Why is that?</p>
  
  <p>** Edit. So far as I can tell, the HttpURLConnection just keeps trying to connect (even with a finite timeout) when the credentials are bad. (I know this because I added the line <code>Log.v("Authentication", "Calling...");</code> in my <code>getPasswordAuthentication()</code> method before returning). I want the connection to stop trying if it fails for bad credentials!</p>