HTTP模拟工具【C#/Winform源码】、Json绑定TreeView控件、使用了MetroModernUI、RestSharp、Dapper.Net、Newtonsoft.Json、SmartThreadPool这几个主要开源框架

时间:2023-03-08 15:53:45
HTTP模拟工具【C#/Winform源码】、Json绑定TreeView控件、使用了MetroModernUI、RestSharp、Dapper.Net、Newtonsoft.Json、SmartThreadPool这几个主要开源框架

HTTP模拟工具

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

项目地址:在文章最底部

来!上图!!
Get请求方式

HTTP模拟工具【C#/Winform源码】、Json绑定TreeView控件、使用了MetroModernUI、RestSharp、Dapper.Net、Newtonsoft.Json、SmartThreadPool这几个主要开源框架

Post请求方式
HTTP模拟工具【C#/Winform源码】、Json绑定TreeView控件、使用了MetroModernUI、RestSharp、Dapper.Net、Newtonsoft.Json、SmartThreadPool这几个主要开源框架

Http请求的核心代码

         /// <summary>
         /// 请求
         /// </summary>
         /// <param name="url"></param>
         /// <param name="jsonData"></param>
         /// <param name="methodType"></param>
         /// <returns></returns>
         private string RequestData(string url, Dictionary<string, string> headerData, string jsonData, Method methodType)
         {
             string urlHead = "";
             string urlTail = "";
             )
             {
                 urlHead = "http://";
                 urlTail = url.Substring("http://".Length);
             }
             )
             {
                 urlHead = "https://";
                 urlTail = url.Substring("https://".Length);
             }
             else
             {
                 urlHead = "http://";
                 urlTail = url;
             }

             ]);
             ]) + urlTail.Split(].Length + ), methodType);
             if (methodType == Method.POST)
                 request.AddParameter("application/json;charset=utf-8", jsonData, ParameterType.RequestBody);

             foreach (var item in headerData)
             {
                 request.AddHeader(item.Key, item.Value);
             }

             // execute the request
             IRestResponse response = client.Execute(request);
             var content = response.Content; // raw content as string

             return content.ToString();
         }

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

         /// <summary>
         /// 绑定树形控件
         /// </summary>
         /// <param name="treeView"></param>
         /// <param name="strJson"></param>
         private void BindTreeView(TreeView treeView, string strJson)
         {
             treeView.Nodes.Clear();

             //string strJson = "1";//抛异常
             //string strJson = "{}";
             //string strJson = "{\"errcode\":0,\"errmsg\":\"查询成功\",\"datas\":[{\"c_ResourceType\":\"BootLogo\",\"c_Url\":\"/Upload/Magazine/4e09315d-7d92-4e6a-984d-80f684a24da8.jpg\"}]}";
             //string strJson = "[{\"DeviceCode\":\"430BE-B3C6A-4E953-9F972-FC741\",\"RoomNum\":\"777\"},{\"DeviceCode\":\"BF79F -09807-EEA31-2499E-31A98\",\"RoomNum\":\"888\"}]";

             if (IsJOjbect(strJson))
             {
                 JObject jo = (JObject)JsonConvert.DeserializeObject(strJson);

                 foreach (var item in jo)
                 {
                     TreeNode tree;
                     if (item.Value.GetType() == typeof(JObject))
                     {
                         tree = new TreeNode(item.Key);
                         AddTreeChildNode(ref tree, item.Value.ToString());
                         treeView.Nodes.Add(tree);
                     }
                     else if (item.Value.GetType() == typeof(JArray))
                     {
                         tree = new TreeNode(item.Key);
                         AddTreeChildNode(ref tree, item.Value.ToString());
                         treeView.Nodes.Add(tree);
                     }
                     else
                     {
                         tree = new TreeNode(item.Key + ":" + item.Value.ToString());
                         treeView.Nodes.Add(tree);
                     }
                 }
             }
             if (IsJArray(strJson))
             {
                 JArray ja = (JArray)JsonConvert.DeserializeObject(strJson);
                 ;
                 foreach (JObject item in ja)
                 {
                     TreeNode tree = new TreeNode("Array [" + (i++) + "]");
                     foreach (var itemOb in item)
                     {
                         TreeNode treeOb;
                         if (itemOb.Value.GetType() == typeof(JObject))
                         {
                             treeOb = new TreeNode(itemOb.Key);
                             AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
                             tree.Nodes.Add(treeOb);

                         }
                         else if (itemOb.Value.GetType() == typeof(JArray))
                         {
                             treeOb = new TreeNode(itemOb.Key);
                             AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
                             tree.Nodes.Add(treeOb);
                         }
                         else
                         {
                             treeOb = new TreeNode(itemOb.Key + ":" + itemOb.Value.ToString());
                             tree.Nodes.Add(treeOb);
                         }
                     }
                     treeView.Nodes.Add(tree);
                 }
             }
             treeView.ExpandAll();
         }

         /// <summary>
         /// 添加子节点
         /// </summary>
         /// <param name="parantNode"></param>
         /// <param name="value"></param>
         public void AddTreeChildNode(ref TreeNode parantNode, string value)
         {
             if (IsJOjbect(value))
             {
                 JObject jo = (JObject)JsonConvert.DeserializeObject(value);
                 foreach (var item in jo)
                 {
                     TreeNode tree;
                     if (item.Value.GetType() == typeof(JObject))
                     {
                         tree = new TreeNode(item.Key);
                         AddTreeChildNode(ref tree, item.Value.ToString());
                         parantNode.Nodes.Add(tree);
                     }
                     else if (item.Value.GetType() == typeof(JArray))
                     {
                         tree = new TreeNode(item.Key);
                         AddTreeChildNode(ref tree, item.Value.ToString());
                         parantNode.Nodes.Add(tree);
                     }
                     else
                     {
                         tree = new TreeNode(item.Key + ":" + item.Value.ToString());
                         parantNode.Nodes.Add(tree);
                     }
                 }
             }
             if (IsJArray(value))
             {
                 JArray ja = (JArray)JsonConvert.DeserializeObject(value);
                 ;
                 foreach (JObject item in ja)
                 {
                     TreeNode tree = new TreeNode("Array [" + (i++) + "]");
                     parantNode.Nodes.Add(tree);
                     foreach (var itemOb in item)
                     {
                         TreeNode treeOb;
                         if (itemOb.Value.GetType() == typeof(JObject))
                         {
                             treeOb = new TreeNode(itemOb.Key);
                             AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
                             tree.Nodes.Add(treeOb);

                         }
                         else if (itemOb.Value.GetType() == typeof(JArray))
                         {
                             treeOb = new TreeNode(itemOb.Key);
                             AddTreeChildNode(ref treeOb, itemOb.Value.ToString());
                             tree.Nodes.Add(treeOb);
                         }
                         else
                         {
                             treeOb = new TreeNode(itemOb.Key + ":" + itemOb.Value.ToString());
                             tree.Nodes.Add(treeOb);
                         }
                     }
                 }
             }
         }

         /// <summary>
         /// 判断是否JOjbect类型
         /// </summary>
         /// <param name="value"></param>
         /// <returns></returns>
         private bool IsJOjbect(string value)
         {
             try
             {
                 JObject ja = JObject.Parse(value);
                 return true;
             }
             catch (Exception ex)
             {
                 return false;
             }
         }

         /// <summary>
         /// 判断是否JArray类型
         /// </summary>
         /// <param name="value"></param>
         /// <returns></returns>
         private bool IsJArray(string value)
         {
             try
             {
                 JArray ja = JArray.Parse(value);
                 return true;
             }
             catch (Exception ex)
             {
                 return false;
             }
         }

下载项目

编译后Debug