• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

HTTP模拟工具【C#/Winform源码】、Json绑定TreeView控件、使用了MetroModernUI、RestS ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

HTTP模拟工具

开发语言:C#/Winform
开发工具:Visual Studio 2017
数据库:   SQLite
使用框架:界面-MetroModernUI
              Http请求-RestSharp
              ORM-Dapper.Net
              Json解析-Newtonsoft.Json
              多线程-SmartThreadPool
本来打算试一下Dapper.Net扩展框架-DapperExtensions,用了有点问题,不知什么原因后来弃用了,用回原生的Dapper.Net。
相关程序包被我删了,NuGet管理器自行还原。

项目地址:在文章最底部

来!上图!!

Get请求方式

Post请求方式

Http请求的核心代码

 1         /// <summary>
 2         /// 请求
 3         /// </summary>
 4         /// <param name="url"></param>
 5         /// <param name="jsonData"></param>
 6         /// <param name="methodType"></param>
 7         /// <returns></returns>
 8         private string RequestData(string url, Dictionary<string, string> headerData, string jsonData, Method methodType)
 9         {
10             string urlHead = "";
11             string urlTail = "";
12             if (url.IndexOf("http://") == 0)
13             {
14                 urlHead = "http://";
15                 urlTail = url.Substring("http://".Length);
16             }
17             else if (url.IndexOf("https://") == 0)
18             {
19                 urlHead = "https://";
20                 urlTail = url.Substring("https://".Length);
21             }
22             else
23             {
24                 urlHead = "http://";
25                 urlTail = url;
26             }
27 
28             var client = new RestClient(urlHead + urlTail.Split('/')[0]);
29             var request = new RestRequest(url.Substring(url.LastIndexOf(urlTail.Split('/')[0]) + urlTail.Split('/')[0].Length + 1), methodType);
30             if (methodType == Method.POST)
31                 request.AddParameter("application/json;charset=utf-8", jsonData, ParameterType.RequestBody);
32 
33             foreach (var item in headerData)
34             {
35                 request.AddHeader(item.Key, item.Value);
36             }
37 
38             // execute the request
39             IRestResponse response = client.Execute(request);
40             var content = response.Content; // raw content as string
41 
42             return content.ToString();
43         }

 

Json数据绑定TreeView控件核心代码

  1         /// <summary>
  2         /// 绑定树形控件
  3         /// </summary>
  4         /// <param name="treeView"></param>
  5         /// <param name="strJson"></param>
  6         private void BindTreeView(TreeView treeView, string strJson)
  7         {
  8             treeView.Nodes.Clear();
  9 
 10             //string strJson = "1";//抛异常
 11             //string strJson = "{}";
 12             //string strJson = "{\"errcode\":0,\"errmsg\":\"查询成功\",\"datas\":[{\"c_ResourceType\":\"BootLogo\",\"c_Url\":\"/Upload/Magazine/4e09315d-7d92-4e6a-984d-80f684a24da8.jpg\"}]}";
 13             //string strJson = "[{\"DeviceCode\":\"430BE-B3C6A-4E953-9F972-FC741\",\"RoomNum\":\"777\"},{\"DeviceCode\":\"BF79F -09807-EEA31-2499E-31A98\",\"RoomNum\":\"888\"}]";
 14 
 15             if (IsJOjbect(strJson))
 16             {
 17                 JObject jo = (JObject)JsonConvert.DeserializeObject(strJson);
 18 
 19                 foreach (var item in jo)
 20                 {
 21                     TreeNode tree;
 22                     if (item.Value.GetType() == typeof(JObject))
 23                     {
 24                         tree = new TreeNode(item.Key);
 25                         AddTreeChildNode(ref tree, item.Value.ToString());
 26                         treeView.Nodes.Add(tree);
 27                     }
 28                     else if (item.Value.GetType() == typeof(JArray))
 29                     {
 30                         tree = new TreeNode(item.Key);
 31                         AddTreeChildNode(ref tree, item.Value.ToString());
 32                         treeView.Nodes.Add(tree);
 33                     }
 34                     else
 35                     {
 36                         tree = new TreeNode(item.Key + ":" + item.Value.ToString());
 37                         treeView.Nodes.Add(tree);
 38                     }
 39                 }
 40             }
 41             if (IsJArray(strJson))
 42             {
 43                 JArray ja = (JArray)JsonConvert.DeserializeObject(strJson);
 44                 int i = 0;
 45                 foreach (JObject item in ja)
 46                 {
 47                     TreeNode tree = new TreeNode("Array [" + (i++) + "]");
 48                     foreach (var itemOb in item)
 49                     {
 50                         TreeNode treeOb;
 51                         if (itemOb.Value.GetType() == typeof(JObject))
 52                         {
 53                             treeOb = new TreeNode(itemOb.Key);
 54                             AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
 55                             tree.Nodes.Add(treeOb);
 56 
 57                         }
 58                         else if (itemOb.Value.GetType() == typeof(JArray))
 59                         {
 60                             treeOb = new TreeNode(itemOb.Key);
 61                             AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
 62                             tree.Nodes.Add(treeOb);
 63                         }
 64                         else
 65                         {
 66                             treeOb = new TreeNode(itemOb.Key + ":" + itemOb.Value.ToString());
 67                             tree.Nodes.Add(treeOb);
 68                         }
 69                     }
 70                     treeView.Nodes.Add(tree);
 71                 }
 72             }
 73             treeView.ExpandAll();
 74         }
 75 
 76         /// <summary>
 77         /// 添加子节点
 78         /// </summary>
 79         /// <param name="parantNode"></param>
 80         /// <param name="value"></param>
 81         public void AddTreeChildNode(ref TreeNode parantNode, string value)
 82         {
 83             if (IsJOjbect(value))
 84             {
 85                 JObject jo = (JObject)JsonConvert.DeserializeObject(value);
 86                 foreach (var item in jo)
 87                 {
 88                     TreeNode tree;
 89                     if (item.Value.GetType() == typeof(JObject))
 90                     {
 91                         tree = new TreeNode(item.Key);
 92                         AddTreeChildNode(ref tree, item.Value.ToString());
 93                         parantNode.Nodes.Add(tree);
 94                     }
 95                     else if (item.Value.GetType() == typeof(JArray))
 96                     {
 97                         tree = new TreeNode(item.Key);
 98                         AddTreeChildNode(ref tree, item.Value.ToString());
 99                         parantNode.Nodes.Add(tree);
100                     }
101                     else
102                     {
103                         tree = new TreeNode(item.Key + ":" + item.Value.ToString());
104                         parantNode.Nodes.Add(tree);
105                     }
106                 }
107             }
108             if (IsJArray(value))
109             {
110                 JArray ja = (JArray)JsonConvert.DeserializeObject(value);
111                 int i = 0;
112                 foreach (JObject item in ja)
113                 {
114                     TreeNode tree = new TreeNode("Array [" + (i++) + "]");
115                     parantNode.Nodes.Add(tree);
116                     foreach (var itemOb in item)
117                     {
118                         TreeNode treeOb;
119                         if (itemOb.Value.GetType() == typeof(JObject))
120                         {
121                             treeOb = new TreeNode(itemOb.Key);
122                             AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
123                             tree.Nodes.Add(treeOb);
124 
125                         }
126                         else if (itemOb.Value.GetType() == typeof(JArray))
127                         {
128                             treeOb = new TreeNode(itemOb.Key);
129                             AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
130                             tree.Nodes.Add(treeOb);
131                         }
132                         else
133                         {
134                             treeOb = new TreeNode(itemOb.Key + ":" + itemOb.Value.ToString());
135                             tree.Nodes.Add(treeOb);
136                         }
137                     }
138                 }
139             }
140         }
141 
142         /// <summary>
143         /// 判断是否JOjbect类型
144         /// </summary>
145         /// <param name="value"></param>
146         /// <returns></returns>
147         private bool IsJOjbect(string value)
148         {
149             try
150             {
151                 JObject ja = JObject.Parse(value);
152                 return true;
153             }
154             catch (Exception ex)
155             {
156                 return false;
157             }
158         }
159 
160         /// <summary>
161         /// 判断是否JArray类型
162         /// </summary>
163         /// <param name="value"></param>
164         /// <returns></returns>
165         private bool IsJArray(string value)
166         {
167             try
168             {
169                 JArray ja = JArray.Parse(value);
170                 return true;
171             }
172             catch (Exception ex)
173             {
174                 return false;
175             }
176         }

 项目地址:https://github.com/Xyzing/MwHttpTool

下载项目

编译后Debug


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Windows下C语言实现HTTP客户端发布时间:2022-07-13
下一篇:
80C51单片机指令的取指、执行时序发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap