码迷,mamicode.com
首页 > Windows程序 > 详细

通过HttpClient来调用Web Api接口

时间:2018-04-01 00:59:57      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:name   OLE   通过   static   异步   string   web api   success   das   

/// <summary>  
/// HttpClient实现Post请求(异步)  
/// </summary>  
static async void dooPost()  
{  
    string url = "http://localhost:52824/api/register";  
     //设置HttpClientHandler的AutomaticDecompression  
    var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };  
    //创建HttpClient(注意传入HttpClientHandler)  
    using (var http = new HttpClient(handler))  
    {  
        //使用FormUrlEncodedContent做HttpContent  
        var content = new FormUrlEncodedContent(new Dictionary<string, string>()         
        {    {"Id","6"},  
             {"Name","添加zzl"},  
             {"Info", "添加动作"}//键名必须为空  
         });  
  
        //await异步等待回应  
  
        var response = await http.PostAsync(url, content);  
        //确保HTTP成功状态值  
        response.EnsureSuccessStatusCode();  
        //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)  
        Console.WriteLine(await response.Content.ReadAsStringAsync());  
    }  
  
}  
/// <summary>  
/// HttpClient实现Get请求(异步)  
/// </summary>  
static async void dooGet()  
{  
    string url = "http://localhost:52824/api/register?id=1";  
    //创建HttpClient(注意传入HttpClientHandler)  
    var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };  
  
    using (var http = new HttpClient(handler))  
    {  
        //await异步等待回应  
        var response = await http.GetAsync(url);  
        //确保HTTP成功状态值  
        response.EnsureSuccessStatusCode();  
  
        //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)  
        Console.WriteLine(await response.Content.ReadAsStringAsync());  
    }  
}  
/// <summary>  
/// HttpClient实现Put请求(异步)  
/// </summary>  
static async void dooPut()  
{  
    var userId = 1;  
    string url = "http://localhost:52824/api/register?userid=" + userId;  
  
    //设置HttpClientHandler的AutomaticDecompression  
    var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };  
    //创建HttpClient(注意传入HttpClientHandler)  
    using (var http = new HttpClient(handler))  
    {  
        //使用FormUrlEncodedContent做HttpContent  
        var content = new FormUrlEncodedContent(new Dictionary<string, string>()         
        {  
           {"Name","修改zzl"},  
           {"Info", "Put修改动作"}//键名必须为空  
        });  
  
        //await异步等待回应  
  
        var response = await http.PutAsync(url, content);  
        //确保HTTP成功状态值  
        response.EnsureSuccessStatusCode();  
        //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)  
        Console.WriteLine(await response.Content.ReadAsStringAsync());  
    }  
}  

 

通过HttpClient来调用Web Api接口

标签:name   OLE   通过   static   异步   string   web api   success   das   

原文地址:https://www.cnblogs.com/softwyy/p/8684792.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!