c#使用HttpClient调用WebApi

2023-05-16

调用WebApi

可以利用HttpClient来进行Web Api的调用。由于WebA Api的调用本质上就是一次普通的发送请求与接收响应的过程,

所有HttpClient其实可以作为一般意义上发送HTTP请求的工具。

复制代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace 自己的名称空间
{
    public class ApiHelper
    {
        /// <summary>
        /// api调用方法/注意一下API地址
        /// </summary>
        /// <param name="controllerName">控制器名称--自己所需调用的控制器名称</param>
        /// <param name="overb">请求方式--get-post-delete-put</param>
        /// <param name="action">方法名称--如需一个Id(方法名/ID)(方法名/?ID)根据你的API灵活运用</param>
        /// <param name="obj">方法参数--如提交操作传整个对象</param>
        /// <returns>json字符串--可以反序列化成你想要的</returns>
        public static string GetApiMethod(string controllerName, string overb, string action, object obj = null)
        {
            Task<HttpResponseMessage> task = null;
            string json = "";
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:****/api/" + controllerName + "/");
            switch (overb)
            {
                case "get":
                    task = client.GetAsync(action);
                    break;
                case "post":
                    task = client.PostAsJsonAsync(action, obj);
                    break;
                case "delete":
                    task = client.DeleteAsync(action);
                    break;
                case "put":
                    task = client.PutAsJsonAsync(action, obj);
                    break;
                default:
                    break;
            }
            task.Wait();
            var response = task.Result;
            if (response.IsSuccessStatusCode)
            {
                var read = response.Content.ReadAsStringAsync();
                read.Wait();
                json = read.Result;
            }
            return json;
        }
    }
}

复制代码

可能需要以下引用集:

System.Net.Http.Formatting.dll

System.Web.Http.dll

*************************************************

C# HttpClient请求Webapi帮助类

引用 Newtonsoft.Json  

复制代码

        // Post请求
        public string PostResponse(string url,string postData,out string statusCode)
        {
            string result = string.Empty;
            //设置Http的正文
            HttpContent httpContent = new StringContent(postData);
            //设置Http的内容标头
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            //设置Http的内容标头的字符
            httpContent.Headers.ContentType.CharSet = "utf-8";
            using(HttpClient httpClient=new HttpClient())
            {
                //异步Post
                HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
                //输出Http响应状态码
                statusCode = response.StatusCode.ToString();
                //确保Http响应成功
                if (response.IsSuccessStatusCode)
                {
                    //异步读取json
                    result = response.Content.ReadAsStringAsync().Result;
                }
            }
            return result;
        }

        // 泛型:Post请求
        public T PostResponse<T>(string url,string postData) where T:class,new()
        {
            T result = default(T);
 
            HttpContent httpContent = new StringContent(postData);
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            httpContent.Headers.ContentType.CharSet = "utf-8";
            using(HttpClient httpClient=new HttpClient())
            {
                HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
 
                if (response.IsSuccessStatusCode)
                {
                    Task<string> t = response.Content.ReadAsStringAsync();
                    string s = t.Result;
                    //Newtonsoft.Json
                    string json = JsonConvert.DeserializeObject(s).ToString();
                    result = JsonConvert.DeserializeObject<T>(json); 
                }
            }
            return result;
        }

        // 泛型:Get请求
        public T GetResponse<T>(string url) where T :class,new()
        {
            T result = default(T);
 
            using (HttpClient httpClient=new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = httpClient.GetAsync(url).Result;
 
                if (response.IsSuccessStatusCode)
                {
                    Task<string> t = response.Content.ReadAsStringAsync();
                    string s = t.Result;
                    string json = JsonConvert.DeserializeObject(s).ToString();
                    result = JsonConvert.DeserializeObject<T>(json);
                }
            }
            return result;
        }

        // Get请求
        public string GetResponse(string url, out string statusCode)
        {
            string result = string.Empty;
 
            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
 
                HttpResponseMessage response = httpClient.GetAsync(url).Result;
                statusCode = response.StatusCode.ToString();
 
                if (response.IsSuccessStatusCode)
                {
                    result = response.Content.ReadAsStringAsync().Result;
                }
            }
            return result;
        }

        // Put请求
        public string PutResponse(string url, string putData, out string statusCode)
        {
            string result = string.Empty;
            HttpContent httpContent = new StringContent(putData);
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            httpContent.Headers.ContentType.CharSet = "utf-8";
            using (HttpClient httpClient = new HttpClient())
            {
                HttpResponseMessage response = httpClient.PutAsync(url, httpContent).Result;
                statusCode = response.StatusCode.ToString();
                if (response.IsSuccessStatusCode)
                {
                    result = response.Content.ReadAsStringAsync().Result;
                }
            }
            return result;
        }

        // 泛型:Put请求
        public T PutResponse<T>(string url, string putData) where T : class, new()
        {
            T result = default(T);
            HttpContent httpContent = new StringContent(putData);
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            httpContent.Headers.ContentType.CharSet = "utf-8";
            using(HttpClient httpClient=new HttpClient())
            {
                HttpResponseMessage response = httpClient.PutAsync(url, httpContent).Result;
 
                if (response.IsSuccessStatusCode)
                {
                    Task<string> t = response.Content.ReadAsStringAsync();
                    string s = t.Result;
                    string json = JsonConvert.DeserializeObject(s).ToString();
                    result = JsonConvert.DeserializeObject<T>(json);
                }
            }
            return result;
        }

复制代码

 

出处:https://blog.csdn.net/sun_zeliang/article/details/81587835

========================================================

我自己把上面的修改下,可以不引用 Newtonsoft.Json  ,在POST模式的方法PostWebAPI增加了GZip的支持,请求超时设置,其他的功能可以自己去扩展,增加了简单调用的方式。

后续可以扩展异步方式、HttpWebRequest方式调用Webapi(待完成。。。)

复制代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;



namespace Car.AutoUpdate.Comm
{
    public class WebapiHelper
    {

        #region HttpClient

        /// <summary>
        /// Get请求指定的URL地址
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <returns></returns>
        public static string GetWebAPI(string url)
        {
            string result = "";
            string strOut = "";
            try
            {
                result = GetWebAPI(url, out strOut);
            }
            catch (Exception ex)
            {
                LogHelper.Error("调用后台服务出现异常!", ex);
            }
            return result;
        }

        /// <summary>
        /// Get请求指定的URL地址
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <param name="statusCode">Response返回的状态</param>
        /// <returns></returns>
        public static string GetWebAPI(string url, out string statusCode)
        {
            string result = string.Empty;
            statusCode = string.Empty;
            try
            {
                using (HttpClient httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                    HttpResponseMessage response = httpClient.GetAsync(url).Result;
                    statusCode = response.StatusCode.ToString();

                    if (response.IsSuccessStatusCode)
                    {
                        result = response.Content.ReadAsStringAsync().Result;
                    }
                    else
                    {
                        LogHelper.Warn("调用后台服务返回失败:" + url + Environment.NewLine + SerializeObject(response));
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("调用后台服务出现异常!", ex);
            }
            return result;
        }

        /// <summary>
        /// Get请求指定的URL地址
        /// </summary>
        /// <typeparam name="T">返回的json转换成指定实体对象</typeparam>
        /// <param name="url">URL地址</param>
        /// <returns></returns>
        public static T GetWebAPI<T>(string url) where T : class, new()
        {
            T result = default(T);
            try
            {
                using (HttpClient httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = httpClient.GetAsync(url).Result;

                    if (response.IsSuccessStatusCode)
                    {
                        Task<string> t = response.Content.ReadAsStringAsync();
                        string s = t.Result;
                        string jsonNamespace = DeserializeObject<T>(s).ToString();
                        result = DeserializeObject<T>(s);
                    }
                    else
                    {
                        LogHelper.Warn("调用后台服务返回失败:" + url + Environment.NewLine + SerializeObject(response));
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("调用后台服务出现异常!", ex);
            }

            return result;
        }

        /// <summary>
        /// Post请求指定的URL地址
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <param name="postData">提交到Web的Json格式的数据:如:{"ErrCode":"FileErr"}</param>
        /// <returns></returns>
        public static string PostWebAPI(string url, string postData)
        {
            string result = "";
            HttpStatusCode strOut = HttpStatusCode.BadRequest;
            try
            {
                result = PostWebAPI(url, postData, out strOut);
            }
            catch (Exception ex)
            {
                LogHelper.Error("调用后台服务出现异常!", ex);
            }
            return result;

        }

        /// <summary>
        /// Post请求指定的URL地址
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <param name="postData">提交到Web的Json格式的数据:如:{"ErrCode":"FileErr"}</param>
        /// <param name="statusCode">Response返回的状态</param>
        /// <returns></returns>
        public static string PostWebAPI(string url, string postData, out HttpStatusCode httpStatusCode)
        {
            string result = string.Empty;
            httpStatusCode = HttpStatusCode.BadRequest;
            //设置Http的正文
            HttpContent httpContent = new StringContent(postData);
            //设置Http的内容标头
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            //设置Http的内容标头的字符
            httpContent.Headers.ContentType.CharSet = "utf-8";

            HttpClientHandler httpHandler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
            try
            {
                //using (HttpClient httpClient = new HttpClient(httpHandler))
                using (HttpClient httpClient = new HttpClient())
                {
                    httpClient.Timeout = new TimeSpan(0, 0, 5);
                    //异步Post
                    HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
                    //输出Http响应状态码
                    httpStatusCode = response.StatusCode;
                    //确保Http响应成功
                    if (response.IsSuccessStatusCode)
                    {
                        //异步读取json
                        result = response.Content.ReadAsStringAsync().Result;
                    }
                    else
                    {
                        LogHelper.Warn("调用后台服务返回失败:" + url + Environment.NewLine + SerializeObject(response));
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("调用后台服务出现异常!", ex);
            }
            return result;
        }

        /// <summary>
        /// Post请求指定的URL地址
        /// </summary>
        /// <typeparam name="T">返回的json转换成指定实体对象</typeparam>
        /// <param name="url">URL地址</param>
        /// <param name="postData">提交到Web的Json格式的数据:如:{"ErrCode":"FileErr"}</param>
        /// <returns></returns>
        public static T PostWebAPI<T>(string url, string postData) where T : class, new()
        {
            T result = default(T);

            HttpContent httpContent = new StringContent(postData);
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            httpContent.Headers.ContentType.CharSet = "utf-8";

            HttpClientHandler httpHandler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
            try
            {
                using (HttpClient httpClient = new HttpClient(httpHandler))
                {
                    HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;

                    if (response.IsSuccessStatusCode)
                    {
                        Task<string> t = response.Content.ReadAsStringAsync();
                        string s = t.Result;
                        //Newtonsoft.Json
                        string jsonNamespace = DeserializeObject<T>(s).ToString();
                        result = DeserializeObject<T>(s);
                    }
                    else
                    {
                        LogHelper.Warn("调用后台服务返回失败:" + url + Environment.NewLine + SerializeObject(response));
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("调用后台服务出现异常!", ex);
            }
            return result;
        }

        /// <summary>
        /// Put请求指定的URL地址
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <param name="putData">提交到Web的Json格式的数据:如:{"ErrCode":"FileErr"}</param>
        /// <returns></returns>
        public static string PutWebAPI(string url, string putData)
        {
            string result = "";
            string strOut = "";
            result = PutWebAPI(url, putData, out strOut);
            return result;
        }

        /// <summary>
        /// Put请求指定的URL地址
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <param name="putData">提交到Web的Json格式的数据:如:{"ErrCode":"FileErr"}</param>
        /// <param name="statusCode">Response返回的状态</param>
        /// <returns></returns>
        public static string PutWebAPI(string url, string putData, out string statusCode)
        {
            string result = statusCode = string.Empty;
            HttpContent httpContent = new StringContent(putData);
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            httpContent.Headers.ContentType.CharSet = "utf-8";
            try
            {
                using (HttpClient httpClient = new HttpClient())
                {
                    HttpResponseMessage response = httpClient.PutAsync(url, httpContent).Result;
                    statusCode = response.StatusCode.ToString();
                    if (response.IsSuccessStatusCode)
                    {
                        result = response.Content.ReadAsStringAsync().Result;
                    }
                    else
                    {
                        LogHelper.Warn("调用后台服务返回失败:" + url + Environment.NewLine + SerializeObject(response));
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("调用后台服务出现异常!", ex);
            }
            return result;
        }

        /// <summary>
        /// Put请求指定的URL地址
        /// </summary>
        /// <typeparam name="T">返回的json转换成指定实体对象</typeparam>
        /// <param name="url">URL地址</param>
        /// <param name="putData">提交到Web的Json格式的数据:如:{"ErrCode":"FileErr"}</param>
        /// <returns></returns>
        public static T PutWebAPI<T>(string url, string putData) where T : class, new()
        {
            T result = default(T);
            HttpContent httpContent = new StringContent(putData);
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            httpContent.Headers.ContentType.CharSet = "utf-8";
            try
            {
                using (HttpClient httpClient = new HttpClient())
                {
                    HttpResponseMessage response = httpClient.PutAsync(url, httpContent).Result;

                    if (response.IsSuccessStatusCode)
                    {
                        Task<string> t = response.Content.ReadAsStringAsync();
                        string s = t.Result;
                        string jsonNamespace = DeserializeObject<T>(s).ToString();
                        result = DeserializeObject<T>(s);
                    }
                    else
                    {
                        LogHelper.Warn("调用后台服务返回失败:" + url + Environment.NewLine + SerializeObject(response));
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("调用后台服务出现异常!", ex);
            }
            return result;
        }

        /// <summary> 
        /// 对象转JSON 
        /// </summary> 
        /// <param name="obj">对象</param> 
        /// <returns>JSON格式的字符串</returns> 
        public static string SerializeObject(object obj)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            try
            {
                return jss.Serialize(obj);
            }
            catch (Exception ex)
            {
                LogHelper.Error("JSONHelper.SerializeObject 转换对象失败。", ex);
                throw new Exception("JSONHelper.SerializeObject(object obj): " + ex.Message);
            }
        }

        /// <summary>
        /// 将Json字符串转换为对像  
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T DeserializeObject<T>(string json)
        {
            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            T objs = default(T);
            try
            {
                objs = Serializer.Deserialize<T>(json);
            }
            catch (Exception ex)
            {
                LogHelper.Error("JSONHelper.DeserializeObject 转换对象失败。", ex);
                throw new Exception("JSONHelper.DeserializeObject<T>(string json): " + ex.Message);
            }
            return objs;

        }

        #endregion

        private static HttpResponseMessage HttpPost(string url, HttpContent httpContent)
        {
            HttpResponseMessage response = null;
            HttpClientHandler httpHandler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
            try
            {
                //using (HttpClient httpClient = new HttpClient(httpHandler))
                using (HttpClient httpClient = new HttpClient())
                {
                    httpClient.Timeout = new TimeSpan(0, 0, 5);
                    //异步Post
                    response = httpClient.PostAsync(url, httpContent).Result;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("调用后台服务出现异常!", ex);
            }
            return response;
        }

    }
}

复制代码

 

下面再分享一个帮助类,有用到的做个参考吧

复制代码

using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace WebCollect.CommonHelp
{
    public static class CommonHelper
    {
        #region HttpClient
        private static HttpClient _httpClient;
        public static HttpClient httpClient
        {
            get
            {
                if (_httpClient == null)
                {
                    _httpClient = new HttpClient();
                    _httpClient.Timeout = new TimeSpan(0, 1, 0);

                }
                return _httpClient;
            }
            set { _httpClient = value; }
        }


        #endregion

        #region get请求
        /// <summary>
        /// get请求返回的字符串
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetRequestStr(string url)
        {
            try
            {
                var response = httpClient.GetAsync(new Uri(url)).Result;
                return response.Content.ReadAsStringAsync().Result;
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// get请求返回的二进制
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static byte[] GetRequestByteArr(string url)
        {
            try
            {
                var response = httpClient.GetAsync(new Uri(url)).Result;
                return response.Content.ReadAsByteArrayAsync().Result;
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion

        #region post请求
        /// <summary>
        /// post请求返回的字符串
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string PostRequestStr(string url)
        {
            try
            {
                string contentStr = "";
                StringContent sc = new StringContent(contentStr);
                sc.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");//todo
                var response = httpClient.PostAsync(new Uri(url), sc).Result;
                return response.Content.ReadAsStringAsync().Result;
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion

    }
}

复制代码

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

c#使用HttpClient调用WebApi 的相关文章

随机推荐

  • MFC和c#中模拟对另一进程的窗口按钮点击

    1 在自动化测试中经常要模拟窗口按钮的点击 参考文章 xff1a http blog csdn net blackboyofsnp article details 3372719 有时我们需要这么做 手动模拟按钮按下的事件 让程序做出与按钮
  • C#中进程间通信方式汇总

    一 进程间通讯的方式 进程间通讯的方式有很多 xff0c 常用的有共享内存 xff08 内存映射文件 共享内存DLL 剪切板等 xff09 命名管道和匿名管道 发送消息等几种方法来直接完成 xff0c 另外还可以通过socket口 配置文件
  • ubuntu shared folder to windows

    一 安装smb 执行命令行 xff1a sudo apt get install samba sudo apt get install smbfs 二 添加准备共享的文件夹 有如下三种配置共享文件夹的方法 xff0c 任选一种方法即可 xf
  • C# 获得窗体句柄并发送消息(利用windows API可在不同进程中获取)

    C 使用Windows API获取窗口句柄控制其他程序窗口 编写程序模拟鼠标和键盘操作可以方便的实现你需要的功能 xff0c 而不需要对方程序为你开放接口 比如 xff0c 操作飞信定时发送短信等 我之前开发过飞信耗子 xff0c 用的是对
  • c#中mysql远程连接方法及实例

    region 远程数据库连接测试 需给远程数据库分配所有权限 cmd命令 xff1a grant all privileges on to 39 root 39 64 39 39 with grant option string connS
  • mysql中数据库覆盖导入的几种方式

    众所周知 xff0c 数据库中INSERT INTO语法是append方式的插入 xff0c 而最近在处理一些客户数据导入场景时 xff0c 经常遇到需要覆盖式导入的情况 xff0c 常见的覆盖式导入主要有下面两种 xff1a 1 部分覆盖
  • mysql并发写入性能分析

    目前 xff0c 在很多OLTP场景中 xff0c MySQL数据库都有着广泛的应用 xff0c 也有很多不同的使用方式 从数据库的业务需求 架构设计 运营维护 再到扩容迁移 xff0c 不同的MySQL架构有不同的特点 xff0c 适应一
  • c#中的DefWndProc是Control类的虚函数

    protected override void DefWndProc ref Message m protected override void DefWndProc ref Message m 是Control的虚函数
  • C#使用Win32API获得窗口和控件的句柄

    整个Windows编程的基础 一个句柄是指使用的一个唯一的整数值 即一个4字节 64位程序中为8字节 长的数值 来标识应用程序中的不同对象和同类中的不同的实例 诸如 一个窗口 按钮 图标 滚动条 输出设备 控件或者文件等 应用程序能够通过句
  • C/C++新建注册表项实例

    使用Windows API 函数中的RegCreateKeyEx函数来实现对注册表新建注册表项 RegCreateKeyEx函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 原形 LONG RegCreateKeyEx
  • c#中通过win32API(FindWindowEx)查找控件句柄实例

    函数功能 该函数获得一个窗口的句柄 该窗口的类名和窗口名与给定的字符串相匹配 这个函数查找子窗口 从排在给定的子窗口后面的下 一个子窗口开始 在查找时不区分大小写 函数原型 HWND FindWindowEx HWND hwndParent
  • c#中使用消息循环机制发送接收字符串的方法和数据类型转换

    在定义消息时忘记了用户可定义消息的边界值 xff0c 在网上一阵疯找后来发现是const int WM USER 61 0x400 接着是SendMessage的lParam类型不能决定 xff08 默认是IntPtr xff09 xff0
  • C#WebApi路由机制详解

    随着前后端分离的大热 WebApi在项目中的作用也是越来越重要 可单独部署 与前端和App交互都很方便 既然有良好的发展趋势 我们当然应该顺势而为 搞懂WebApi Restful相当于给Http请求增加了语义 Post 增 Delete
  • xubuntu(ubuntu)重启后不能进入图形化界面

    问题描述 xff1a Xbuntu启动后进入了 VMware Easy Install视图 xff0c 不能进入图形化界面 问题思路 xff1a 在命令行模式下命令联想敲击时会报硬盘容量不足 xff0c 怀疑可能和硬盘大小相关 先尝试清理下
  • JSON数据格式详解

    文章目录 JSON数据格式概念 JSON的简单数据 JSON对象 对象的属性也可以是JSON对象 JSON格式表示简单数组 对象数组 使用二维数组保存 二维数组 访问淘宝的接口也可以取得JSON格式的数据 将一个对象转换成JSON数据 将一
  • C# 创建一个简单的WebApi项目

    一 创建Web API 1 创建一个新的web API项目 启动VS 2013 并在 开始页 选择 新项目 或从 文件 菜单选择 新建 然后选择 项目 在 模板 面板中选择 已安装模板 并展开 Visual C 节点 选择该节点下的 Web
  • C# 编写Web API

    1 创建Web API项目 打开VS2012 gt FILE gt New gt Project gt Web gt ASP NET MVC 4 Web Application 修改名字为WebAPIApplication 单击OK 在Pr
  • C# WebApi 返回JSON类型

    在默认情况下 当我们新建一个webapi项目 会自动返回XML格式的数据 如果我们想返回JSON的数据 可以设置下面的三种方法 nbsp 1 不用改配置文件 在Controller的方法中 直接返回HttpResponseMessage p
  • c#通过HttpClient来调用Web Api接口

    lt summary gt HttpClient实现Post请求 异步 lt summary gt static async void dooPost string url http localhost 52824 api register
  • c#使用HttpClient调用WebApi

    调用WebApi 可以利用HttpClient来进行Web Api的调用 由于WebA Api的调用本质上就是一次普通的发送请求与接收响应的过程 xff0c 所有HttpClient其实可以作为一般意义上发送HTTP请求的工具 using