title: HTTP Digest Authentication with Parameters
body: <p>we are trying to connect to a WebService using digest authentication.
  It's working fine as long as I don't have any parameters in the uri.
  I am searching for a solution since days.</p>
  
  <p>I also tried many examples I found like:<br>
  <a href="http://blogs.msdn.com/b/daniem/archive/2013/02/27/digest-authentication-in-system-net-classes-not-compliant-with-rfc2617.aspx" rel="nofollow">http://blogs.msdn.com/b/daniem/archive/2013/02/27/digest-authentication-in-system-net-classes-not-compliant-with-rfc2617.aspx</a></p>
  
  <p>However I always get 401 Not authorized.</p>
  
  <p>See the attached code:</p>
  
  <pre><code>private string WebServiceCall(string methodname)
      {
          try
          {
              string response = string.Empty;
  
              Uri uri = new Uri(string.Format("http://{0}:{1}/{2}", servername, port, methodname));
              UriBuilder builder = new UriBuilder(uri);
              var query = HttpUtility.ParseQueryString(builder.Query);
              query["fields"] = "posgood";
              builder.Query = query.ToString();
              uri = builder.Uri;
  
              WebProxy proxy = new WebProxy(uri, true);
  
              HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
              webRequest.Method = "GET";
              webRequest.Proxy = proxy;
              webRequest.PreAuthenticate = true;
              webRequest.AllowAutoRedirect = true;
              CredentialCache cache = new CredentialCache();
              cache.Add(new Uri(string.Format("http://{0}:{1}/{2}", servername, port, methodname)), "Digest", new NetworkCredential("myUser", "myPassowrd", "Ekahau RTLS Controller"));
              webRequest.Credentials = cache;
  
  
              using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
              using (Stream responseStream = webResponse.GetResponseStream())
              {
                  if (responseStream != null)
                  {
                      using (StreamReader streamReader = new StreamReader(responseStream))
                      {
                          response = streamReader.ReadToEnd();
                      }
                  }
              }
              return response;
          }
          catch (Exception caught)
          {
              throw new Exception(string.Format("Exception in WebServiceCall: {0}", caught.Message));
          }
      }
  </code></pre>
  
  <p>If you outcomment the UriBuilder part, it's working fine.<br>
  I also tried to use HttpClient.GetAsync() method with the same result.</p>
  
  <p><strong>EDIT 1:</strong>
  <br>
  Uri without parameters: 172.16.24.51:8550/epe/pos/taglist
   <br>
  Uri with parameters: 172.16.24.51:8550/epe/pos/taglist?fields=posgood</p>