title: (401) Unauthorized Error When Calling Web API from a Console Application
body: <p>When I call my WEB API from my Console Application, I encounter:</p>
  
  <p><strong>The remote server returned an error: (401) Unauthorized.</strong></p>
  
  <p>This application runs in Interanet (<strong>Windows Authentication</strong>)</p>
  
  <pre><code>            Uri uri = new Uri("http://myServer/api/main/foo");
              WebClient client = new WebClient();
              client.Credentials = CredentialCache.DefaultCredentials;
  
              using (Stream data = client.OpenRead(uri))
              {
                  using (StreamReader sr = new StreamReader(data))
                  {
                      string result = sr.ReadToEnd();
                      Console.WriteLine(result);
                  }
              }
  </code></pre>
  
  <p><strong>Updated</strong></p>
  
  <p>If I replace  </p>
  
  <blockquote>
    <p>client.Credentials = CredentialCache.DefaultCredentials;</p>
  </blockquote>
  
  <p>with this line </p>
  
  <blockquote>
    <p>client.Credentials = new NetworkCredential( username, password);</p>
  </blockquote>
  
  <p>it works fine but I need the current credential to be set automatically.</p>
  
  <p>Any idea? 
  Thanks in advance ;)</p>