Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Consume Web API from .NET Console application

$
0
0

In the old .NET version, we can consume Web Api from

HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:30151/");

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        //List all Customers
        HttpResponseMessage response = client.GetAsync("api/customers").Result;

         if (response.IsSuccessStatusCode)
        {
            var customers = response.Content.ReadAsAsync<IEnumerable<Customer>>().Result;
            foreach (var c in customers)
            {
                Console.WriteLine("ID: {0}\tName: {1}\tAddress: {2}\tEmail: {3}\tPhone: {4}", c.id, c.name, c.address, c.email, c.phone);
            }
        }
        else
        {
            Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
        }

        Console.Read();

Now if using .net coreclr. Is there a replacement for that?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>