当前位置: 首页 > news >正文

C# 使用RestSharp 开发WebApi client端,实现MES通信

1, 使用 nuget  添加第三方库

2,准备好服务器的 url ,用户名、密码等信息,上代码:

    var options = new RestClientOptions("http://www.ebmes.net:10800/oauth/token");var client = new RestClient(options);var request = new RestRequest();request.Method = Method.Post;request.AddParameter("Content-Type", "application/json");request.AddParameter("username", "aoi_client");request.AddParameter("password", "Aoi_Client_202207");request.AddParameter("grant_type", "password");request.AddParameter("client_id", "aoi_client");request.AddParameter("client_secret", "Aoi202207");var response = await client.PostAsync(request);Console.WriteLine(response.Content);var token = JsonConverer.DeserializeJsonString<Token>(response.Content);_token = token.access_token;Console.WriteLine(_token);

3,通过登录获取到的 token ,用 Post   推送信息:

            var options = new RestClientOptions("http://www.ebmes.net:10800/gateway/openapi/dc/eb-cc-loading/save-pressure");var client = new RestClient(options);var request = new RestRequest();request.AddParameter("Content-Type", "application/json");request.AddHeader("Authorization", $"Bearer {_token}");request.AddHeader("ACTOR_USERNAME","aoi_client");request.Method = Method.Post;string body = GetPressuerInfo();request.AddJsonBody(body);var response = await client.PostAsync(request);Console.WriteLine(response);


http://www.mrgr.cn/news/10403.html

相关文章:

  • 【漏洞复现】SuiteCRM responseEntryPoint Sql注入漏洞
  • mac安装java17(jdk17)
  • Spring Boot密码加密
  • Agent实际落地的应用 未来生活的无形助手
  • 基于改进YOLOv8的景区行人检测算法
  • 数据结构-串-了解串-串的基本操作
  • 信号的产生
  • SQL Server事务日志文件过大的处理方法
  • 计算机网络-数据链路层
  • Nginx反向代理B
  • DVWA靶场通关(CSRF)
  • 常用的接口测试的方法有哪些?接口测试质量评估的标准是什么?
  • 【初阶数据结构】顺序表与链表的比较(附题)
  • 链接 -- 动静态链接 --特点、区别、静态库安装下载
  • PyTorch踩坑记录1
  • 谷粒商城实战笔记-239-商城业务-购物车-ThreadLocal用户身份鉴别
  • 使用CORS解决跨域问题
  • springboot社区帮扶对象管理系统论文源码调试讲解
  • 如何了解SSL证书链情况?
  • Mybatis-plus 创建自定义 FreeMarker 模板详细教程