欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C#使用RestClient调用Web API

时间:2022-12-21 09:27:19|栏目:.NET代码|点击:

获取Token

		/// <summary>
        /// 获取Token
        /// </summary>
        /// <returns></returns>
        public static async Task<string> GetToken()
        {
            var url = $"http://127.0.0.1/terminal-tenant/sys/login";
            var client = new RestClient(url);
            var request = new RestRequest(Method.POST);
            request.RequestFormat = DataFormat.Json;
            var body = new { username = "xxx", password = "xxx" };
            request.AddParameter("application/json; charset=utf-8", JsonConvert.SerializeObject(body), ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            var responseContent = response.Content;
            return responseContent;
        }

保存数据

		/// <summary>
        /// 保存数据
        /// </summary>
        /// <returns></returns>
        public async Task<string> Save(Model model, string token)
        {
            var url = $"http://127.0.0.1/terminal-tenant/app/visitor/save";
            var client = new RestClient(url);
            var request = new RestRequest(Method.POST);
            request.RequestFormat = DataFormat.Json;
            request.AddHeader("token", token);
            request.AddParameter("application/json; charset=utf-8", JsonConvert.SerializeObject(model), ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            var responseContent = response.Content;
            return responseContent;
        }

上传文件

		/// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="bt"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<string> Upload(byte[] bt, string token)
        {
            var url = $"http://127.0.0.1/terminal-tenant/tenant/common/upload";
            var client = new RestClient(url);
            var request = new RestRequest(Method.POST);
            request.RequestFormat = DataFormat.Json;
            request.AddHeader("token", token);
            request.AddFile("file", bt, "multipart/form-data");
            IRestResponse response = client.Execute(request);
            var responseContent = response.Content;
            return responseContent;
        }

上一篇:ASP.NET MVC3 实现全站重定向的简单方法

栏    目:.NET代码

下一篇:没有了

本文标题:C#使用RestClient调用Web API

本文地址:http://www.codeinn.net/misctech/221846.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有