通过HttpClient来调用Web Api接口

Posted 东方甲乙木

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过HttpClient来调用Web Api接口相关的知识,希望对你有一定的参考价值。

/// <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接口的主要内容,如果未能解决你的问题,请参考以下文章

调用接口,解析Json字符串并存入数据库

从 ASP.net 4 Web API 2 项目通过 HttpClient 调用 REST 服务导致异常

.NET Core Web API使用HttpClient提交文件的二进制流(multipart/form-data内容类型)

如何使用 HttpClient 从 Web Api 调用 PUT 方法?

Fiddler 没有看到来自 C# HttpClient() 的 API 调用

如何使用带有 SSL 的 httpclient 调用 SOAP Web 服务