实例介绍
【实例简介】
【实例截图】
【核心代码】
using HISWebInterface.Model; using Newtonsoft.Json; using RDH.Data; using RDH.Data.Access; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Web; namespace HISWebInterface { /// <summary> /// Service 的摘要说明 /// </summary> public class Service : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; try { Stream stream = context.Request.InputStream; if (stream.Length > 0) { StreamReader reader = new StreamReader(stream); string str = reader.ReadToEnd(); if (VerifySignature(str)) { Json json = JsonConvert.DeserializeObject<Json>(str); if (json.InterfaceUserId == ConfigHellper.InterfaceUserId) { switch (json.Method) { case "ruidaheng_get_userInfo": context.Response.Write(ToJson(json.Method, 0, "", 0, "", GetUserInfo())); break; case "ruidaheng_get_deptInfo": context.Response.Write(ToJson(json.Method, 0, "", 0, "", GetDeptInfo())); break; default: context.Response.Write(ToJson(json.Method, -1, "没有相应接口", -1, "", "")); break; } } else { context.Response.Write(ToJson(json.Method, -1, "用户错误", -1, "", "")); } } else { context.Response.Write(ToJson("", -1, "验证失败", -1, "", "")); } } else { context.Response.Write(ToJson("", -1, "请求的数据不能为空!", -1, "", "")); } } catch (Exception ex) { context.Response.Write(ToJson("", -1, ex.Message, -1, "", "")); } } public bool IsReusable { get { return false; } } //将结果转化为JSON格式 private String ToJson(string Method, int ReturnCode, string ReturnMess, int ResultCode, string ResultMess, String items) { Dictionary<string, string> dics = new Dictionary<string, string>(); dics.Add("ReturnCode", ReturnCode.ToString()); dics.Add("ReturnMess", ReturnMess); if (ReturnCode == 0) { dics.Add("InterfaceUserId", ConfigHellper.InterfaceUserId); dics.Add("NonceStr", GenerateMD5("ruidaheng_get_deptInfo" DateTime.Now)); dics.Add("ResultCode", ResultCode.ToString()); dics.Add("ResultMess", ResultMess); dics.Add("Items", items); string str = getParamSrc(dics); str = str "&key=" ConfigHellper.WebKey; string sign = GenerateMD5(str).ToUpper(); dics.Add("Sign", sign); dics.Remove("Items"); dics.Add("Items", items.Replace("\"", "\\\"")); } string result = "{\"" String.Join("\",\"", dics.Select(x => string.Join("\":\"", x.Key, x.Value))) "\"}"; return result; } /// <summary> /// 查询用户信息 /// </summary> /// <returns>JSON格式的字符串</returns> private String GetUserInfo() { List<UserInfo> users = null; using (CommonConnnectionSessionScope scope = new CommonConnnectionSessionScope(ConfigHellper.SateServerConnection)) { string strsql = "select * from UserInfo_V "; users = SqlMapper.Query<UserInfo>(ConnectionFactory.Current.GetSessionConnection(), strsql) .ToList(); } return JsonConvert.SerializeObject(users); } /// <summary> /// 查询部门信息 /// </summary> /// <returns>JSON格式的字符串</returns> private String GetDeptInfo() { List<DeptInfo> depts = null; using (CommonConnnectionSessionScope scope = new CommonConnnectionSessionScope(ConfigHellper.SateServerConnection)) { string strsql = "select * from OrganizeInfo_V t"; depts = SqlMapper.Query<DeptInfo>(ConnectionFactory.Current.GetSessionConnection(), strsql) .ToList(); } return JsonConvert.SerializeObject(depts); } /// <summary> /// 验证签名,确保数据准确 /// </summary> /// <param name="str"></param> /// <returns></returns> private bool VerifySignature(string str) { Dictionary<string, string> dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(str); string resultSign = ""; dict.TryGetValue("Sign", out resultSign); dict.Remove("Sign"); string resultStr = getParamSrc(dict);// String.Join("&", dict.Select(x => string.Join("=", x.Key, x.Value))); resultStr = resultStr "&key=" ConfigHellper.WebKey; string sign = GenerateMD5(resultStr).ToUpper(); return resultSign == sign; } /// <summary> /// 将字典转化为需要的字符串 /// </summary> /// <param name="paramsMap"></param> /// <returns></returns> private string getParamSrc(Dictionary<string, string> paramsMap) { var vDic = (from objDic in paramsMap orderby objDic.Key ascending select objDic); StringBuilder str = new StringBuilder(); foreach (KeyValuePair<string, string> kv in vDic) { string key = kv.Key; string value = kv.Value; if (!string.IsNullOrEmpty(value)) str.Append(key "=" value "&"); } String result = str.ToString().Substring(0, str.ToString().Length - 1); return result; } /// <summary> /// MD5字符串加密 /// </summary> /// <param name="txt"></param> /// <returns>加密后字符串</returns> private string GenerateMD5(string strText) { using (var md5 = MD5.Create()) { var signDataBytes = md5.ComputeHash(Encoding.GetEncoding("utf-8").GetBytes(strText)); return BitConverter.ToString(signDataBytes).Replace("-", ""); } } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论