title: WebRequests in C# to a REST API server
body: <p>I am totally new to WebRequests. I need to implement several WebRequests to receive json files from a server. From the server I know that: </p>
  
  <p>Authentication:</p>
  
  <blockquote>
    <p>Authentication of each request is done via a token. The system administrator will provide you with
    your token, which must be used with each request. The token should be kept secure (like a
    password) as it gives anyone in it's possession with the full access that the associated user would
    have.</p>
  </blockquote>
  
  <p>Headers</p>
  
  <blockquote>
    <p>Standard HTTP headers may be used to facilitate connection keep-alive, caching, compression etc.
    All requests require the following headers</p>
    
    <p>Accept: application/json</p>
    
    <p>Authorization: &lt; authentication token ></p>
  </blockquote>
  
  <p>I searched for examples and I found a WebRequest implementation which I tried:</p>
  
  <pre><code>        public string LoadHttpPageWithBasicAuthentication(string url, string username, string password)
      {
          Uri myUri = new Uri(url);
          WebRequest myWebRequest = HttpWebRequest.Create(myUri);
  
          HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
          myHttpWebRequest.Method = "GET";
          myHttpWebRequest.ContentType = ".json";
  
          NetworkCredential myNetworkCredential = new NetworkCredential(username, password);
  
          CredentialCache myCredentialCache = new CredentialCache();
          myCredentialCache.Add(myUri, "Basic", myNetworkCredential);
  
          myHttpWebRequest.PreAuthenticate = true;
          myHttpWebRequest.Credentials = myCredentialCache;
  
          WebResponse myWebResponse = myWebRequest.GetResponse();
  
          Stream responseStream = myWebResponse.GetResponseStream();
  
          StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);
  
          string pageContent = myStreamReader.ReadToEnd();
  
          responseStream.Close();
  
          myWebResponse.Close();
  
          return pageContent;
      }
  </code></pre>
  
  <p>And I call it by</p>
  
  <pre><code>            string ret=Request.LoadHttpPageWithBasicAuthentication(@"https://epica.ecninside.ecn.nl/lots", "aaa", "ccc");
          File.WriteAllText("c:\\MyTransfer\\content.html", ret);
  </code></pre>
  
  <p>Unfortunately, my content is always the normal login page instead of any usefull data. If I use the browser, this login page appears as well at first. After login, I receive the data. But how can I handle the whole procedure by my client in c#? </p>