title: How can I use http auth information from a URL in Android?
body: <p>In my browser, or in iOS, when I try to get the contents of a URL with encoded http authentication information in the form </p>
  
  <pre><code>http://myUser:myPassword@www.example.com/secure/area/index.html
  </code></pre>
  
  <p>It just works.  I'm getting URLs from a web service, and I'd like to avoid trying to parse them up for their HTTP auth info if I can help it.  Is there a way to do something similar in Android without actually parsing the URLs?  Alternatively, what is the best way to go about that?</p>
  
  <p>UPDATE: 
  I find that when I try to set the authentication information in an Authorization header, I get a very strange FileNotFoundException.</p>
  
  <p>Here's the code I'm using:</p>
  
  <pre><code>        URL url = new URL(urlString);
          URLConnection connection;
          String authority = url.getAuthority();
          if (authority.contains("@")) {
              String userPasswordString = authority.split("@")[0];
              url = new URL(urlString.replace(userPasswordString + "@", ""));
              connection = url.openConnection();
              String encoded = new String(Base64.encode(userPasswordString.getBytes(), Base64.DEFAULT), "UTF-8");
              connection.setRequestProperty("Authorization", "Basic " + encoded);
          } else {
              connection = url.openConnection();
          }
  
          InputStream responseStream = connection.getInputStream();
  </code></pre>
  
  <p>All the info seems to check out, I've verified the url is correct, the base64 string is correct, and the file is certainly on the server--I have no trouble at all opening it with Firefox, and Firebug shows all the right headers, matching what I've sent as far as I can tell.  What I get though is the following error (url host changed to protect the innocent):</p>
  
  <pre><code>java.io.FileNotFoundException: http://a1b.example.com/grid/uploads/profile/avatar/user1/custom-avatar.jpg
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1061)
  </code></pre>
  
  <p>Any idea what this is all about?</p>
  
  <p>I looked into using HttpClient, but saw that in <a href="http://code.google.com/p/android/issues/detail?id=16041#c1" rel="nofollow">Issue 16041</a> it is recommended that we prefer URLConnection.</p>