title: Login with HTTPS to htaccess protected directory
body: <p>the app i am currently building needs to perform a login with username and password. I am using the DefaultHttpClient to execute the request and setting the username and password as credentials.
  the server expects a htacces-login.
  The Result i get is not the Logintoken i hoped for. Instead it returns an Errormessage.
  Can anybody tell me where i went wrong in my Code?
  Thank you so much for your help!
  pr</p>
  
  <pre><code>private String httpLoginRequest() {
      String loginToken = null;
      String loginUrl = "https://somedomain.com/login";
  
      DefaultHttpClient client = new DefaultHttpClient();
      client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials("username", "password"));
      HttpResponse httpResponse;
      HttpEntity entity;
      try{
          httpResponse = client.execute(request);
          entity = httpResponse.getEntity();
          if (entity != null) {
              InputStream instream = entity.getContent();
              loginToken = convertInputStreamToString(instream);
          }
      } catch (ClientProtocolException e) {
          client.getConnectionManager().shutdown();
          e.printStackTrace();
      } catch (IOException e) {
          client.getConnectionManager().shutdown();
          e.printStackTrace();
      }
      return loginToken;
  }
  </code></pre>
  
  <p>Thank you guys so much for your help. I finally figured it out. It was a mix of different things:
  1. what you cant see in the code above (because i removed it by accident) is that the request is post. our api expects a get request though i was told otherwise
  2. i put the credentials in the wrong place. they have to be set in the HttpGet-Object as in the Exampe given by CommonsWare
  3. i actually do have to encode the credentials with base64 mentioned by you</p>
  
  <p>here is the code that finally worked for me:</p>
  
  <pre><code>public String httpLoginRequest() {
      String loginToken = null;
      String loginUrl = "https://somedomain.com/login";
      //
      DefaultHttpClient client = new DefaultHttpClient();
      HttpResponse httpResponse;
      HttpEntity entity;
      HttpGet request = new HttpGet(loginUrl);
      request.addHeader("Authorization", "Basic " + Base64.encodeCredentials("username", "password"));
      try{
          httpResponse = client.execute(request);
          entity = httpResponse.getEntity();
          if (entity != null) {
              InputStream instream = entity.getContent();
              loginToken = convertInputStreamToString(instream);
              Log.d(LOG_TAG, "Login Token: " + loginToken);
          }
      } catch (ClientProtocolException e) {
          client.getConnectionManager().shutdown();
          e.printStackTrace();
      } catch (IOException e) {
          client.getConnectionManager().shutdown();
          e.printStackTrace();
      }
      return loginToken;
  }
  </code></pre>
  
  <p>When i find the time i will try the example given in the answer as i looks a lot more like what i will need for some other requests in my project.</p>