title: How can I do digest authentication with HttpWebRequest?
body: <p>Various articles (<a href="https://web.archive.org/web/20211020134945/https://www.4guysfromrolla.com/articles/102605-1.aspx" rel="noreferrer">1</a>, <a href="http://blogs.msdn.com/b/buckh/archive/2004/07/28/199706.aspx" rel="noreferrer">2</a>) I discovered make this look easy enough:</p>
  
  <pre><code>WebRequest request = HttpWebRequest.Create(url);
  
  var credentialCache = new CredentialCache();
  credentialCache.Add(
    new Uri(url), // request url
    "Digest", // authentication type
    new NetworkCredential("user", "password") // credentials
  );
  
  request.Credentials = credentialCache;
  </code></pre>
  
  <p>However, this only works for URLs without URL parameters. For example, I can download <code>http://example.com/test/xyz.html</code> just fine, but when I attempt to download <code>http://example.com/test?page=xyz</code>, the result is a 400 Bad Request message with the following in the server's logs (running Apache 2.2):</p>
  
  <pre><code>Digest: uri mismatch - &lt;/test&gt; does not match request-uri &lt;/test?page=xyz&gt;
  </code></pre>
  
  <p>My first idea was that the digest specification requires URL parameters to be removed from the digest hash -- but removing the parameter from the URL passed to <code>credentialCache.Add()</code> didn't change a thing. So it must be the other way around and somewhere in the .NET framework is wrongly removing the parameter from the URL.</p>