zoukankan      html  css  js  c++  java
  • HttpClient使用

    using System;
    using System.Net;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Threading.Tasks;
    
    namespace HttpClientSample
    {
        public class Product
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public decimal Price { get; set; }
            public string Category { get; set; }
        }
    
        class Program
        {
            static HttpClient client = new HttpClient();
    
            static void ShowProduct(Product product)
            {
                Console.WriteLine($"Name: {product.Name}	Price: " +
                    $"{product.Price}	Category: {product.Category}");
            }
    
            static async Task<Uri> CreateProductAsync(Product product)
            {
                HttpResponseMessage response = await client.PostAsJsonAsync(
                    "api/products", product);
                response.EnsureSuccessStatusCode();
    
                // return URI of the created resource.
                return response.Headers.Location;
            }
    
            static async Task<Product> GetProductAsync(string path)
            {
                Product product = null;
                HttpResponseMessage response = await client.GetAsync(path);
                if (response.IsSuccessStatusCode)
                {
                    product = await response.Content.ReadAsAsync<Product>();
                }
                return product;
            }
    
            static async Task<Product> UpdateProductAsync(Product product)
            {
                HttpResponseMessage response = await client.PutAsJsonAsync(
                    $"api/products/{product.Id}", product);
                response.EnsureSuccessStatusCode();
    
                // Deserialize the updated product from the response body.
                product = await response.Content.ReadAsAsync<Product>();
                return product;
            }
    
            static async Task<HttpStatusCode> DeleteProductAsync(string id)
            {
                HttpResponseMessage response = await client.DeleteAsync(
                    $"api/products/{id}");
                return response.StatusCode;
            }
    
            static void Main()
            {
                RunAsync().GetAwaiter().GetResult();
            }
    
            static async Task RunAsync()
            {
                // Update port # in the following line.
                client.BaseAddress = new Uri("http://localhost:64195/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
    
                try
                {
                    // Create a new product
                    Product product = new Product
                    {
                        Name = "Gizmo",
                        Price = 100,
                        Category = "Widgets"
                    };
    
                    var url = await CreateProductAsync(product);
                    Console.WriteLine($"Created at {url}");
    
                    // Get the product
                    product = await GetProductAsync(url.PathAndQuery);
                    ShowProduct(product);
    
                    // Update the product
                    Console.WriteLine("Updating price...");
                    product.Price = 80;
                    await UpdateProductAsync(product);
    
                    // Get the updated product
                    product = await GetProductAsync(url.PathAndQuery);
                    ShowProduct(product);
    
                    // Delete the product
                    var statusCode = await DeleteProductAsync(product.Id);
                    Console.WriteLine($"Deleted (HTTP Status = {(int)statusCode})");
    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
    
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    基本类型和包装类对象使用 == 和 equals进行比较的结果?
    ==和equals的区别是什么?
    JDK和JRE有什么区别?
    Java自学指南三、入门视频优先
    Java自学指南二、后端开发全景图与快速入门
    Java自学指南一、找一个开始并能坚持下去的理由
    什么是&#160;happens-before 原则?
    什么是 Java 内存模型?
    Java 中有哪些无锁技术来解决并发问题?如何使用?
    什么是活锁和饥饿?
  • 原文地址:https://www.cnblogs.com/zmaiwxl/p/8932592.html
Copyright © 2011-2022 走看看