title: Apache HttpClient Digest authentication
body: <p>Basically what I need to do is to perform digest authentication. First thing I tried is the official example available <a href="http://svn.apache.org/repos/asf/httpcomponents/httpclient/tags/4.0.1/httpclient/src/examples/org/apache/http/examples/client/ClientAuthentication.java" rel="noreferrer">here</a>. 
  But when I try to execute it(with some small changes, Post instead of the the Get method) I get a</p>
  
  <pre><code>org.apache.http.auth.MalformedChallengeException: missing nonce in challange
  at org.apache.http.impl.auth.DigestScheme.processChallenge(DigestScheme.java:132)
  </code></pre>
  
  <p>When this failed I tried using:</p>
  
  <pre><code>DefaultHttpClient client = new DefaultHttpClient();
  client.getCredentialsProvider().setCredentials(new AuthScope(null, -1, null), new UsernamePasswordCredentials("&lt;username&gt;", "&lt;password&gt;"));
  
  HttpPost post = new HttpPost(URI.create("http://&lt;someaddress&gt;"));
          List&lt;NameValuePair&gt; nvps = new ArrayList&lt;NameValuePair&gt;();
  nvps.add(new BasicNameValuePair("domain", "&lt;username&gt;"));
  post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
  
  DigestScheme digestAuth = new DigestScheme();
  digestAuth.overrideParamter("algorithm", "MD5");
  digestAuth.overrideParamter("realm", "http://&lt;someaddress&gt;");
  digestAuth.overrideParamter("nonce", Long.toString(new Random().nextLong(), 36));
  digestAuth.overrideParamter("qop", "auth");
  digestAuth.overrideParamter("nc", "0");
  digestAuth.overrideParamter("cnonce", DigestScheme.createCnonce());
  
  Header auth = digestAuth.authenticate(new
        UsernamePasswordCredentials("&lt;username&gt;", "&lt;password&gt;"), post);
  System.out.println(auth.getName());
  System.out.println(auth.getValue());
  post.setHeader(auth);
  
  
  HttpResponse ret = client.execute(post);
  ByteArrayOutputStream v2 = new ByteArrayOutputStream();
  ret.getEntity().writeTo(v2);
  System.out.println("----------------------------------------");
  System.out.println(v2.toString());
  System.out.println("----------------------------------------");
  System.out.println(ret.getStatusLine().getReasonPhrase());
  System.out.println(ret.getStatusLine().getStatusCode());
  </code></pre>
  
  <p>At first I have only overridden "realm" and "nonce" DigestScheme parameters. But it turned out that PHP script running on the server requires all other params, but no matter if I specify them or not DigestScheme doesn't generate them in the Authorization RequestPreperty when I call its authenticate() method. And PHP script returns HTTP response code 200 with a message that PHP script requires cnonce, nc and qop parameters.</p>
  
  <p>I've been struggling with this for two days, and no luck. Based on everything I think that the cause of the problem is the PHP script. It looks to me that it doesn't send a challenge when app tries to access it unauthorized.</p>
  
  <p>Any ideas anyone?</p>
  
  <p>Edit:
  One more thing, I've tried connecting with cURL and it works.</p>