/// <summary> /// 获取post提交信息过来的json数据 /// </summary> /// <param name="context">http请求信息</param> /// <param name="name">接口名称</param> /// <returns>返回一个键值集合</returns> public static Dictionary <string, string> GetRequestJson(HttpContext context, string name) { try { string jsonstr = ""; using (var wr = new StreamReader(context.Request.InputStream)) { jsonstr = wr.ReadToEnd(); } try { Dictionary <string, string> sArray = new Dictionary <string, string>(); sArray = JMP.TOOL.JsonHelper.Deserialize <Dictionary <string, string> >(jsonstr); return(sArray); } catch (Exception ex) { AddLocLog.AddLog(1, 4, HttpContext.Current.Request.UserHostAddress, name + "通知接口错误", "获取参数json参数转换失败!报错信息:" + ex.ToString() + ",传入参数:" + jsonstr);//写入报错日志 return(null); } } catch (Exception ex) { AddLocLog.AddLog(1, 4, HttpContext.Current.Request.UserHostAddress, name + "通知接口错误", "获取参数json参数失败!报错信息:" + ex.ToString());//写入报错日志 return(null); } }
/// <summary> /// 将Dictionary键值集合转换成json格式 /// </summary> /// <param name="sb">Dictionary键值集合</param> /// <param name="ts">是否需要组装二级json格式,如果需要则传入需要组装二级json字段的名字(如键值集合中的key值为a需要把a组装成二级json格式,那么就传入a结果{"b": "b","a": { "a": "a"}}注意a的value需要自己组装成json格式或者字符串 )</param> /// <returns></returns> public static string DictJsonstr(Dictionary <string, string> sb, string ts = null) { string result = ""; try { if (sb != null) { result = "{"; result += string.Join(",", sb.Select(x => !string.IsNullOrEmpty(ts) && ts == x.Key ? "\"" + x.Key + "\":" + x.Value : "\"" + x.Key + "\":\"" + x.Value + "\"")); result += "}"; } } catch (Exception ex) { AddLocLog.AddLog(1, 4, HttpContext.Current.Request.UserHostAddress, "json转换数据错误", "将键值集合转换为json出错!错误信息:" + ex.ToString()); //写入报错日志 } return(result); }
/// <summary> /// 获取post提交的数据流 /// </summary> /// <param name="context"></param> /// <returns></returns> public static string PostInput(HttpContext context, string name) { try { System.IO.Stream s = context.Request.InputStream; int count = 0; byte[] buffer = new byte[1024]; StringBuilder builder = new StringBuilder(); while ((count = s.Read(buffer, 0, 1024)) > 0) { builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); } s.Flush(); s.Close(); s.Dispose(); return(builder.ToString()); } catch (Exception ex) { AddLocLog.AddLog(1, 4, HttpContext.Current.Request.UserHostAddress, name + "通知接口错误", "获取参数json参数失败!报错信息:" + ex.ToString());//写入报错日志 return(""); } }
/// <summary> /// 获取get提交信息 /// </summary> /// <param name="context"></param> /// <returns>返回一个键值集合</returns> public static Dictionary <string, string> GetRequestGet(HttpContext context, string name) { try { int i = 0; Dictionary <string, string> sArray = new Dictionary <string, string>(); NameValueCollection coll; //Load Form variables into NameValueCollection variable. coll = context.Request.QueryString; // Get names of all forms into a string array. String[] requestItem = coll.AllKeys; for (i = 0; i < requestItem.Length; i++) { sArray.Add(requestItem[i], context.Request.QueryString[requestItem[i]]); } return(sArray); } catch (Exception ex) { AddLocLog.AddLog(1, 4, HttpContext.Current.Request.UserHostAddress, name + "通知接口错误", "获取参数json参数失败!报错信息:" + ex.ToString());//写入报错日志 return(null); } }