title: Consuming WEB API using HttpClient in c# console application
body: <p>I have created a web API in visual studio 2015 using a MySQL database. The API is working perfect.</p>
  <p>So I decided to make a console <a href="http://www.c-sharpcorner.com/article/web-api-with-httpclient-or-consume-web-api-from-console-appl/" rel="nofollow noreferrer">client</a> application in which I can consume my web-service (web API). The client code is based on <code>HttpClient</code>, and in the API I have used <code>HttpResponse</code>. Now when I run my console application code, I get nothing. Below is my code:</p>
  <p><strong>Class</strong></p>
  <pre><code>class meters_info_dev
  {
      public int id { get; set; }
      public string meter_msn { get; set; }
      public string meter_kwh { get; set; }
  }
  </code></pre>
  <p>This class is same as in my web API model class:</p>
  <p><strong>Model in web API</strong></p>
  <pre><code>namespace WebServiceMySQL.Models
  {
  
  using System;
  using System.Collections.Generic;
  
  public partial class meters_info_dev
  {
      public int id { get; set; }
      public string meter_msn { get; set; }
      public string meter_kwh { get; set; }
  }
  </code></pre>
  <p><strong>Console application code</strong></p>
  <pre><code>static HttpClient client = new HttpClient();
  
  static void ShowAllProducts(meters_info_dev mi)
  {
      Console.WriteLine($&quot;Meter Serial Number:{mi.meter_msn}\t Meter_kwh: {mi.meter_kwh}&quot;, &quot;\n&quot;);
  }
  
  static async Task&lt;List&lt;meters_info_dev&gt;&gt; GetAllRecordsAsync(string path)
  {
      List&lt;meters_info_dev&gt; mID = new List&lt;meters_info_dev&gt;();
      HttpResponseMessage response = await client.GetAsync(path);
      if (response.IsSuccessStatusCode)
      {
          mID = await response.Content.ReadAsAsync&lt;List&lt;meters_info_dev&gt;&gt;();
      }
      else
      {
          Console.WriteLine(&quot;No Record Found&quot;);
      }
      return mID;
  }
  
  static void Main()
  {
      RunAsync().Wait();
  }
  
  static async Task RunAsync()
  {
      client.BaseAddress = new Uri(&quot;http://localhost:2813/&quot;);
      client.DefaultRequestHeaders.Accept.Clear();
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(&quot;application/json&quot;));
  
      var m = await GetAllRecordsAsync(&quot;api/metersinfo/&quot;);
  
      foreach(var b in m)
      {
          ShowAllProducts(b);
      }
  }
  </code></pre>
  <p>In my API I have 3 <code>GET</code> methods under a single controller, so I have created different routes for them. Also the URL for them is different.</p>
  <ol>
  <li><code>http://localhost:2813/api/metersinfo/</code> will return all records</li>
  </ol>
  <p>While debugging the code, I found that <code>List&lt;meters_info_dev&gt; mID = new List&lt;meters_info_dev&gt;();</code> is empty:</p>
  <p><a href="https://I.stack.imgur.com/EGvMd.png" rel="nofollow noreferrer"><img src="https://I.stack.imgur.com/EGvMd.png" alt="enter image description here" /></a></p>
  <p>While the response is <code>302 Found</code>, the URL is also correct:</p>
  <p><a href="https://I.stack.imgur.com/gVf3s.png" rel="nofollow noreferrer"><img src="https://I.stack.imgur.com/gVf3s.png" alt="enter image description here" /></a></p>
  <h2>Update 1</h2>
  <p>After a suggestion I have done the following:</p>
  <pre><code>using (var client = new HttpClient())
  {
      List&lt;meters_info_dev&gt; mID = new List&lt;meters_info_dev&gt;();
      HttpResponseMessage response = await client.GetAsync(path);
      if (response.IsSuccessStatusCode)
      {
          mID = await response.Content.ReadAsAsync&lt;List&lt;meters_info_dev&gt;&gt;();
      }
      else
      {
          Console.WriteLine(&quot;No Record Found&quot;);
      }
     return mID;
  }
  </code></pre>
  <p>When I run the application, I get the exception &quot;An invalid request URI was provided. The request URI must either be an absolute URI or <code>BaseAddress</code> must be set.&quot;</p>
  <h2>Update 2</h2>
  <p>I have added a new piece of code:</p>
  <pre><code>using (var cl = new HttpClient())
  {
      var res = await cl.GetAsync(&quot;http://localhost:2813/api/metersinfo&quot;);
      var resp = await res.Content.ReadAsStringAsync();
  }
  </code></pre>
  <p>And in the response I am getting all the records:</p>
  <p><a href="https://I.stack.imgur.com/yhaQJ.png" rel="nofollow noreferrer"><img src="https://I.stack.imgur.com/yhaQJ.png" alt="enter image description here" /></a></p>
  <p>I don't know why it's not working with the other logic and what the problem is. I have also read the questions <a href="https://stackoverflow.com/questions/44077362/httpclient-consume-web-api-via-console-app-c-sharp">Httpclient consume web api via console app C#</a> and <a href="https://stackoverflow.com/questions/42264345/consuming-api-in-console-application">Consuming Api in Console Application</a>.</p>
  <p>Any help would be highly appreciated.</p>