title: Apache Http Digest Authentication using Java
body: <p>I am currently working on a Java project and I can't get the http digest authentication working. I tried using the Apache website, but it didn't help. I have a site that requires HTTP digest authentication.</p>
  
  <pre><code>        DefaultHttpClient httpclient = new DefaultHttpClient();
          String hostUrl = "http://somewebsite.com";
          String postUrl = "http://somewebsite.com/request";
          HttpPost httpPost = new HttpPost(postUrl);
          String username = "hello";
          String password = "world";
          HttpHost targetHost = new HttpHost(hostUrl);
  
          httpclient.getCredentialsProvider().setCredentials(
                  new AuthScope(hostUrl, AuthScope.ANY_PORT),
                  new UsernamePasswordCredentials(username, password));
  
          AuthCache authCache = new BasicAuthCache();
  
          DigestScheme digestAuth = new DigestScheme();
  
          digestAuth.overrideParamter("realm", "some realm");
  
          digestAuth.overrideParamter("nonce", "whatever");
          authCache.put(targetHost, digestAuth);
  
          BasicHttpContext localcontext = new BasicHttpContext();
          localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
  
          // List&lt;NameValuePair&gt; nvps = new ArrayList&lt;NameValuePair&gt;();
          // nvps.add(new BasicNameValuePair("username", "shirwa99@gmail.com"));
          // nvps.add(new BasicNameValuePair("password", "example"));
          // httpPost.setEntity(new UrlEncodedFormEntity(nvps));
          HttpResponse response2 = httpclient.execute(httpPost);
  </code></pre>