Winform中怎样使用HttpClient调用http的get和post接口并将接口返回json数据解析为实体类
场景
需要在Winform的页面初始化之后,调用http的接口,并将返回的json数据
解析,赋值到窗体中的TextBox组件中。
使用接口测试工具测试的接口返回数据如下
注:
博客:
霸道流氓气质的博客_CSDN博客-C#,架构之路,SpringBoot领域博主
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
1、调用get接口
var httpClient = new HttpClient();var url = new Uri("http://后台接口ip:8091/monitor/pathInfo/get?serverName=zlw");var response = httpClient.GetAsync(url).Result;var data = response.Content.ReadAsStringAsync().Result;
这里传递参数直接在url中拼接的方式传递,传递了一个参数为serverName,值为zlw
其中HttpClient需要引用
using System.Net.Http;
此时data的返回数据是json字符串,示例结果
{"msg":"操作成功","code":200,"data":{"id":7,"serverName":"zlw","mineCode":"","targetServer":"","port":"","user":"","pass":"","dir":"","filePath":"","uploadTime":"","uploadStatus":"","remark":"","creatDate":"2022-04-13 00:00:00","updateDate":"","extend":"","extend_2":"","extend_3":""}}
2、调用post接口并传参
var url = new Uri("http://后台接口Ip:8091/monitor/pathInfo/update");var body = new FormUrlEncodedContent(new Dictionary<string, string>{{"serverName", "zlw"},{"mineCode", this.textBox_mineCode.Text.Trim().ToString()},{"targetServer",this.textBox_targetServer.Text.Trim().ToString()},{"port", this.textBox_port.Text.Trim().ToString()},{"user", this.textBox_user.Text.Trim().ToString()},{"pass", this.textBox_pass.Text.Trim().ToString()},{"dir", this.textBox_dir.Text.Trim().ToString()},{"filePath", this.textBox_filePath.Text.Trim().ToString()},});// responsevar response = httpClient.PostAsync(url, body).Result;var data = response.Content.ReadAsStringAsync().Result;
传递参数通过这种键值对的方式进行赋值。
3、怎样将返回的json字符串数据进行解析为实例类。
首先获取到示例的返回json数据,然后通过JSON转换C#实体类的一些在线网站
Json在线生成C#实体类,Json生成C# Model类工具 - 在线工具(nhooo.com)
JSON转C#实体类|在线JSON转C#工具 - JSON.cn
进行转换成实体类
比如上面转换的实体类就是
namespace bdtdDataUpload
{public class Data{/// <summary>////// </summary>public int id { get; set; }/// <summary>////// </summary>public string serverName { get; set; }/// <summary>////// </summary>public string mineCode { get; set; }/// <summary>////// </summary>public string targetServer { get; set; }/// <summary>////// </summary>public string port { get; set; }/// <summary>////// </summary>public string user { get; set; }/// <summary>////// </summary>public string pass { get; set; }/// <summary>////// </summary>public string dir { get; set; }/// <summary>////// </summary>public string filePath { get; set; }/// <summary>////// </summary>public string uploadTime { get; set; }/// <summary>////// </summary>public string uploadStatus { get; set; }/// <summary>////// </summary>public string remark { get; set; }/// <summary>////// </summary>public string creatDate { get; set; }/// <summary>////// </summary>public string updateDate { get; set; }/// <summary>////// </summary>public string extend { get; set; }/// <summary>////// </summary>public string extend_2 { get; set; }/// <summary>////// </summary>public string extend_3 { get; set; }}public class Root{/// <summary>/// 操作成功/// </summary>public string msg { get; set; }/// <summary>////// </summary>public int code { get; set; }/// <summary>////// </summary>public Data data { get; set; }}}
转换之后注意类名与文件名一致。
然后在获取到结果时解析为实体类
var data = response.Content.ReadAsStringAsync().Result;JavaScriptSerializer js = new JavaScriptSerializer();//实例化一个能够序列化数据的类Root result = js.Deserialize<Root>(data);
这里的Root就是上面生成的实体类
这里需要引用
using System.Web.Script.Serialization;
然后就可以获取到对应的数据进行使用了
var response = httpClient.GetAsync(url).Result;var data = response.Content.ReadAsStringAsync().Result;//Console.WriteLine(data);JavaScriptSerializer js = new JavaScriptSerializer();//实例化一个能够序列化数据的类Root result = js.Deserialize<Root>(data);this.textBox_mineCode.Text = result.data.mineCode;this.textBox_targetServer.Text = result.data.targetServer;this.textBox_port.Text = result.data.port;this.textBox_user.Text = result.data.user;this.textBox_pass.Text = result.data.pass;this.textBox_dir.Text = result.data.dir;this.textBox_filePath.Text = result.data.filePath;
Winform中怎样使用HttpClient调用http的get和post接口并将接口返回json数据解析为实体类相关推荐
- java调用远程 接口_java通过url调用远程接口返回json数据
java通过url调用远程接口返回json数据,有用户名和密码验证, 转自 https://blog.csdn.net/wanglong1990421/article/details/78815856 ...
- python返回json数据_python和flask中返回JSON数据的方法
在python中可以使用json将数据格式化为JSON格式: 1.将字典转换成JSON数据格式: s=['张三','年龄','姓名'] t={} t['data']=s return json.dum ...
- Java调用接口获取json数据解析后保存到数据库
文章目录 1.在yml文件中配置自己定义的接口URL 2.在Controller中添加请求方法和路径 3.在Service中添加方法 4.在ServiceImpl中实现方法 今天给大家带来一个调用接口 ...
- spark中读取json_【spark】文件读写和JSON数据解析
1.读文件 通过 sc.textFile("file://")方法来读取文件到rdd中. val lines = sc.textFile("file://")/ ...
- Flask框架(flask中设置响应信息的方法,返回json数据的方法)
设置响应信息的方法 1. 返回自定义的响应头,有两种方式: (1) 第一种是:视图函数return的时候,使用元组,返回自定义的信息 返回的时候的状态码可以自定义信息:&quo ...
- Spring MVC Controller中返回json数据中文乱码处理
问题 在使用spring MVC Controller的过程中,发现返回到客户端的的中文出现乱码.后台Java代码: @RequestMapping(value = "/upload&quo ...
- java学习笔记——众筹项目练习——项目中的忘记密码(密码找回)与记住我(自动登录)功能 和 返回json数据低版本浏览器显示下载问题的解决、众筹系统架构重构
忘记密码(密码找回) 忘记密码(密码找回)这个功能可以说是目前所有为别人提供服务的软件系统都具备的基础功能啦!很普遍,因为总 ...
- java 控制器 返回 json_SpringMVC中controller返回json数据的方法
java 控制器 返回 json_SpringMVC中controller返回json数据的方法 1.方法一: 使用springmvc原生注解@ResponseBody @ResponseBody @ ...
- Java中怎样将Json字符串转换成实体类
场景 在Java中调用接口获取Json数据后,怎样转换成对应的实体类进行接受与存储. 实现 打开浏览器输入在线Json格式化,这里推荐使用: http://www.bejson.com/ 选择Json ...
最新文章
- 配置访问oracle_SpringBoot中application.properties的常用配置
- leetcode算法题--重建二叉树
- MATLAB信号处理工具箱函数列表分类
- 【插件开发】—— 13 GEF双击模型事件
- 从零开始学产品第五篇:三个环境,开发、测试和线上
- element ui表单处理的简洁方法
- 基于Linux的小项目-在线词典
- js日历控件源代码下载
- 你知道如何提升JSON.stringify()的性能吗?
- pygame.mixer.music
- 角度和弧度的相互换算
- 苹果4S恢复模式 一直正在等待iphone解决办法
- Gartner发布2021年新兴技术成熟度曲线,三大新兴技术趋势主题
- 前端开发常用PhotoShop快捷键整理(更新中)
- IDEA 新手使用手册
- Windows10安装Ubuntu子系统+配置SSH连接+安装xfce4图形界面+配置xrdc远程桌面连接
- Linux系统网络基础
- 【三维点云】CC教程1(Context Capture)
- 用模式识别实现围棋转接器的开源Java项目——一个类似GTool2.exe的工具
- wacom怎么调压感_手绘板压感是什么 数位板压感怎么调【教程】
热门文章
- MySQL客户端的进阶操作
- Samsung/三星I869 Galaxy Win root教程_方法
- 软考(12)-综合布线技术
- C语言打印心形(采用空格与星号)
- 两张图片重叠 CSS
- qt设计为磨砂玻璃效果_Pixyz的2020.1更新为数据准备带来了磨砂和光泽
- 2021内蒙古师大附中高考成绩查询,内蒙古师大附中成绩公布,呼和浩特最好的4所高中,高考大比拼!...
- 计算机颅面研究,计算机辅助颅面复原技术研究
- Android 去掉大括号
- photoshop 快捷键_如何自定义Photoshop的面板,快捷方式和菜单