Consuming Rest Api using Httpclient from dotnet

Consuming Rest Api using Httpclient from dotnet

Recently I had to consume a external rest api using httpclient from .Net, after reading this two articles I have kind change the way i use using { }.

  1. Accessing httpclient wrong with using {    }  
  2. Dispose Httpclient or have a static instance 

Snippet how access the http client is below


    public class OrderController : Controller

        private static HttpClient Client = new HttpClient();
        private static string URL = "";
        private static string _response;

        public OrderController(HttpClient Client)
           _response = Client.GetStringAsync(new Uri(URL)).Result;

        public JsonResult Index()
            //convert string to json array
            JArray boatlist = JArray.Parse(_response);
           // if need to convert to Boat Object
           // var list = JsonConvert.DeserializeObject<List<Boat>>(_response);

           // var jsonresult = JsonConvert.SerializeObject(list);

            return Json(boatlist);




Leave a Reply

Your email address will not be published. Required fields are marked *