title: HTTP GET Request with Username & Password
body: <p>This is my first attempt at creating a <code>console app</code> which can make a <code>HTTP GET</code> request and print the response to the <code>console</code>.  </p>
  
  <p>Thus far, the code works, but only for <code>URIs</code> which do not require a <code>username/password</code>.</p>
  
  <p>My ultimate purpose is to use a <code>cloud/hosting API</code> which accepts <code>HTTP GET</code> requests as <em>triggers</em> for taking certain actions.  As such, I have to use a <code>username/password</code> for this.</p>
  
  <pre><code>using System;
  using System.Net;
  using System.IO;
  
  namespace HttpTestProject {
  
      class Program {
  
          static void Main(string[] args) {
  
              Uri uri = new Uri("http://www.google.com");
              string username = "asdf";
              string password = "asdf";
  
              NetworkCredential cred = new NetworkCredential(username, password);
              CredentialCache cache = new CredentialCache();
              cache.Add(uri, "Basic", cred);
  
              HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
              HttpWebResponse response = (HttpWebResponse)request.GetResponse();
              Stream resStream = response.GetResponseStream();
  
              StreamReader reader = new StreamReader(resStream);
              string text = reader.ReadToEnd();
  
              Console.WriteLine(text);
              Console.ReadLine();
  
          }
  
      }
  
  }
  </code></pre>