static void PrintResult(string note, PostResult entity) { Console.WriteLine(note); if (entity.IsSuccess) { if (0 != entity.Data.Rows.Count) { // 未加密 Console.WriteLine("执行结果:" + entity.IsSuccess + ",数据总数:" + entity.Data.Rows.Count); } else { if (!string.IsNullOrEmpty(entity.Des)) { // 已加密 Console.WriteLine("执行结果:" + entity.IsSuccess + ",解密前:" + entity.Des + ",解密后:" + entity.Info); } else { Console.WriteLine("执行结果:" + entity.IsSuccess + ",返回信息:" + entity.Info); } } } else { Console.WriteLine("执行结果:" + entity.IsSuccess + ",错误信息:" + entity.ErrMsg); } Console.WriteLine(""); }
static void Main(string[] args) { Console.WriteLine("RestClient Start, host:" + host + "\r\n"); // 127.0.0.1或局域网ip或广域网ip rest = new RestDAO(host); entity = rest.Test(); PrintResult("测试接口", entity); entity = rest.MultiParam("A", "B"); PrintResult("参数接口", entity); entity = rest.GetDataTable("select * from tb_user"); PrintResult("获取数据(未加密)接口", entity); entity = rest.GetDataTable_DES("select * from tb_user"); PrintResult("获取数据(DES加密)接口", entity); entity = rest.ExecuteNonQuery("update tb_user set fi_Pwd='123456' where fi_ID=1"); PrintResult("执行操作(未加密)接口", entity); entity = rest.ExecuteNonQuery_DES("update tb_user set fi_Pwd='123456' where fi_ID=1"); PrintResult("执行操作(DES加密)接口", entity); Console.ReadLine(); }
public PostResult GetDataTable_DES(string strSql) { // 加密 strSql = SecurityHelper.EncryptDES(SecurityHelper.key, strSql); string strJson = "{\"strSql\":\"" + strSql + "\"}"; PostResult ret = HttpPost("GetDataTable_DES", strJson); // 解密 ret.Des = ret.Info; ret.Info = SecurityHelper.DecryptDES(SecurityHelper.key, ret.Info); return(ret); }
PostResult HttpPost(string method, string param) { PostResult ret = new PostResult(); try { // 返回结果 // {"code":0,"info":"ok","msg":"","data":null} // {"code":0,"info":"ok","msg":"","data":[{"field1":"value10","field2":"value20"},{"field1":"value11","field2":"value21"}]} WebClient client = new WebClient(); client.Encoding = System.Text.Encoding.UTF8; client.Headers.Add("Content-Type", "application/json"); string jsonBack = client.UploadString(m_strUrl + method, "POST", param); jsonBack = jsonBack.Replace(@"\", ""); // "{\"code\":0,\"info\":\"ok\",\"msg\":\"\",\"data\":null}" jsonBack = jsonBack.Substring(1, jsonBack.Length - 2); JObject jsonInfo = (JObject)JsonConvert.DeserializeObject(jsonBack); if (jsonBack.Contains("code")) { if (0 == jsonInfo.Value <int>("code")) { ret.IsSuccess = true; } ret.Info = jsonInfo.Value <string>("info"); ret.ErrMsg = jsonInfo.Value <string>("errmsg"); JArray arrayData = jsonInfo.Value <JArray>("data"); if (null != arrayData) { ret.Data = JsonConvert.DeserializeObject <DataTable>(arrayData.ToString()); } } } catch (Exception ex) { ret.ErrMsg = ex.Message; } return(ret); }