title: Base64 Encoding Basic Authentication Header Apache HTTP Client
body: <p>Two related questions, I'm using Apache HTTP Client 4.x API. myHttpPost is an instance of HttpPost and myHttpClient is an instance of HttpClient. I'm trying to send a request using basic authentication.  So I have a HttpClient and create a HttpPost. </p>
  
  <p>The 'brute force' way of setting a basic authentication header seems to be to set it in the HttpPost header.</p>
  
  <pre><code>String encoding = Base64Encoder.encode("username" + ":" + "password");
  myHttpPost.setHeader("Authorization", "Basic " + encoding);
  </code></pre>
  
  <p>The example above came from another stack overflow question (can't find link to it now). In relation to the Base64Encoder class - which package would I find it in or where would I download it from?</p>
  
  <p>Main question - I was hoping to do basic authentication in a more aesthetic manner using the code below:</p>
  
  <pre><code>myHttpClient.getCredentialsProvider().setCredentials(
      new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthPolicy.BASIC),
      new UsernamePasswordCredentials("username", "password")
  );
  </code></pre>
  
  <p>But this doesn't seem to work.  So is the first example above the right way to do basic authentication with Apache HTTP Client 4.0?  Or is there a cleaner/simpler way.</p>