Exemple #1
0
        static async Task <Uri> CreateAsync(FlightsManagementSystem.Country country, HttpClient client)
        {
            HttpResponseMessage response = await client.PostAsJsonAsync(
                "api/countries", country);

            response.EnsureSuccessStatusCode();

            // return URI of the created resource.
            return(response.Headers.Location);
        }
Exemple #2
0
        // GET REQUEST
        public void AddCountriesToDB(int countries)
        {
            int        range  = countries;
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(URL);

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            // List data response.
            HttpResponseMessage response = client.GetAsync("").Result;  // Blocking call! Program will wait here until a response is received or a timeout occurs.

            if (response.IsSuccessStatusCode)
            {
                // Parse the response body.
                var dataObjects = response.Content.ReadAsAsync <IEnumerable <Country> >().Result;

                foreach (var d in dataObjects)
                {
                    if (countriesCounter < range)
                    {
                        FlightsManagementSystem.Country country = new FlightsManagementSystem.Country();
                        countriesList.Add(d.name);
                        country.CountryName = d.name;
                        countryDAOMSSQL.Add(country);
                        countriesCounter++;
                        counter++;
                        Thread.Sleep(20);
                    }
                }
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }

            //Make any other calls using HttpClient here.

            //Dispose once all HttpClient calls are complete. This is not necessary if the containing object will be disposed of; for example in this case the HttpClient instance will be disposed automatically when the application terminates so the following call is superfluous.
            client.Dispose();
        }