zoukankan      html  css  js  c++  java
  • WebApi~通过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()); }

    转载:https://blog.csdn.net/smartsmile2012/article/details/51613596

    同步请求:

             /// <returns></returns>
             public static string DoorAllList()
             {
                 var result = "";
                 string url = "http://www.baidu.com/Api/GetDoorUserList";
                 //创建HttpClientHandler的AutomaticDecompression
                 var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
                 //创建一个httpClient;
                 using (var client = new HttpClient(handler))
                {
                     //同步请求
                     var response = client.GetAsync(url);
                     //在这里会等待response返回。
                     var ret = response.Result;
                     //最终等待返回结果
                    result = ret.Content.ReadAsStringAsync().Result;
                 }
                return result;
            }
  • 相关阅读:
    zabbix 二 zabbix agent 客户端
    zabbix (一:zabbix服务端)
    ssh ip "WARING:REMOTE HOST IDENTIFICATION HAS CHANGED!"
    undefined reference to libiconv_open'
    linux 远程 windows 命令:rdesktop vs windows mstsc
    源码安装mysql
    svn 结合rsync 的代码发布系统
    svn !
    使用rsync 的 --delete参数删除目标目录比源目录多余的文件
    svn 提交冲突(目录下删除文件)
  • 原文地址:https://www.cnblogs.com/AndyChen2015/p/9582998.html
Copyright © 2011-2022 走看看