HTTP simulation tool [C #/Winform source code], Json binding TreeView control, using the MetroModernUI, RestSharp, Dapper. Net, Newtonsoft. Json, SmartThreadPool the main open-source frameworks,
HTTP simulation tool
Development language: C #/Winform
Development tools: Visual Studio 2017
Database: SQLite
Usage framework: interface-MetroModernUI
Http request-RestSharp
ORM-Dapper.Net
Json parsing-Newtonsoft. Json
Multithreading-SmartThreadPool
I was going to try Dapper. Net extension framework-DapperExtensions. It was a bit of a problem. I don't know why it was discarded and used back to native Dapper. Net.
I deleted the related packages and restored them by the NuGet manager.
Project address: at the bottom of the article
Come on !!!
Get Request Method
Post Request Method
Core Http Request Code
1 /// <summary> 2 /// request 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 else23 {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 request39 IRestResponse response = client. execute (request); 40 var content = response. content; // raw content as string41 42 return content. toString (); 43}
Core code of the TreeView control bound to Json data
1 /// <summary> 2 // bind the tree control 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"; // throw an exception 11 // string strJson = "{}"; 12 // string strJson = "{\" errcode \ ": 0, \" errmsg \ ": \" query successful \ ", \" datas \": [{\ "c_ResourceType \": \ "BootLogo \", \ "c_Url \": \ "/Upload/Magazine/4e09315d-7d92-4e6a-984d-80f684a24da8.jpg \"}]} "; 13 // string strJson = "[{\" DeviceCode \ ": \" describe-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 // Add a subnode 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 else102 {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 elseft-{ 134 treeOb = new TreeNode (itemOb. key + ":" + itemOb. value. toString (); 135 tree. nodes. add (treeOb ); 136} 137} 138} 139} 140 141 142 // <summary> 143 // determine whether the JOjbect type is 144 /// </summary> 145 // <param name = "value"> </param> 146 // <returns> </returns> 147 private bool IsJOjbect (string value) 148 {149 try150 {151 JObject ja = JObject. parse (value); 152 return true; 153} 154 catch (Exception ex) 155 {156 return false; 157} 158} 159 160 161 // <summary> 162 // determine whether the JArray type is 163 /// </summary> // <param name = "value"> </param> 164 // <returns> </returns> 165 private bool IsJArray (string value) 166 {167 try168 {169 JArray ja = JArray. parse (value); 170 return true; 171} 172 catch (Exception ex) 173 {174 return false; 175} 176}
Download Project
Debug after compilation