Exemplo n.º 1
0
        static async Task RunAsync()
        {
            Console.WriteLine("Calling the back-end API");

            string apiBaseAddress = "http://localhost:43326/";

            CustomDelegatingHandler customDelegatingHandler = new CustomDelegatingHandler();

            HttpClient client = HttpClientFactory.Create(customDelegatingHandler);

            var order = new Order {
                OrderID = 10248, CustomerName = "Taiseer Joudeh", ShipperCity = "Amman", IsShipped = true
            };

            HttpResponseMessage response = await client.PostAsJsonAsync(apiBaseAddress + "api/orders", order);

            if (response.IsSuccessStatusCode)
            {
                string responseString = await response.Content.ReadAsStringAsync();

                Console.WriteLine(responseString);
                Console.WriteLine("HTTP Status: {0}, Reason {1}. Press ENTER to exit", response.StatusCode, response.ReasonPhrase);
            }
            else
            {
                Console.WriteLine("Failed to call the API. HTTP Status: {0}, Reason {1}", response.StatusCode, response.ReasonPhrase);
            }

            Console.ReadLine();
        }
Exemplo n.º 2
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("Calling the back-end API");
            //http://localhost:60806/api/values
            var url = "http://localhost:56349/" + "api/values";

            var customDelegatingHandler = new CustomDelegatingHandler();

            var client = HttpClientFactory.Create(customDelegatingHandler);

            HttpResponseMessage getResponse = await client.GetAsync(url);

            if (getResponse.IsSuccessStatusCode)
            {
                string responseString = await getResponse.Content.ReadAsStringAsync();

                Console.WriteLine(responseString);
                Console.WriteLine("GET - HTTP Status: {0}, Reason {1}", getResponse.StatusCode, getResponse.ReasonPhrase);
            }
            else
            {
                Console.WriteLine("Failed to call the API. HTTP Status: {0}, Reason {1}", getResponse.StatusCode, getResponse.ReasonPhrase);
            }

            var order = new Order {
                OrderID = 10248, CustomerName = "Taiseer Joudeh", ShipperCity = "Amman", IsShipped = true
            };
            string json    = JsonConvert.SerializeObject(order);
            var    content = new StringContent(json, Encoding.UTF8, "application/json");
            HttpResponseMessage postResponse = await client.PostAsync(url, content);

            if (postResponse.IsSuccessStatusCode)
            {
                string responseString = await postResponse.Content.ReadAsStringAsync();

                Console.WriteLine("POST - HTTP Status: {0}, Reason {1}. Press ENTER to exit", postResponse.StatusCode, postResponse.ReasonPhrase);
            }
            else
            {
                Console.WriteLine("Failed to call the API. HTTP Status: {0}, Reason {1}", postResponse.StatusCode, postResponse.ReasonPhrase);
            }

            Console.ReadLine();
        }