/// <summary> /// 获取一个参数的集合列表,该参数集合包含有Get请求参数也包含有POST请求的参数 /// </summary> /// <param name="request">请求页面</param> /// <param name="call">一个检查回调</param> /// <returns></returns> public static IWebParamData GetAllWebParam(this HttpRequestBase request, Func <string, bool> call) { WebParamData _query_dic = new Saker.Web.WebParamData(true); var method = request.HttpMethod; foreach (var r in request.QueryString.AllKeys) { if (string.IsNullOrEmpty(r)) { continue; } var key = r.Trim(); if (!call(key)) { continue; } _query_dic[key] = request.QueryString[key]; } foreach (var r in request.Form.AllKeys) { if (string.IsNullOrEmpty(r)) { continue; } var key = r.Trim(); if (!call(key)) { continue; } _query_dic[key] = request.Form[key]; } return(_query_dic); }
/// <summary> /// 输出一个新的 WebParam 对象; /// </summary> /// <param name="ignoreCase">指示新对象是否不区分大小写</param> /// <returns></returns> public virtual IWebParamData ToNewWebParamData(bool ignoreCase) { var new_o = new WebParamData(ignoreCase); foreach (var s in this) { new_o[s.Key] = s.Value; } return(new_o); }
/// <summary> /// 将参数合并到Url连接中 /// </summary> /// <param name="url">需要合并参数的url</param> /// <param name="param">需要合并的参数</param> /// <param name="isCover">指定使用覆盖模式,如果需要用合并参数覆盖原url参数则为 true。如果使用原url参数覆盖合并参数则为 false </param> /// <returns></returns> public static string MergeUrlQueryString(string url, IWebParamData param, bool isCover = false) { if (param == null) { return(url); } if (string.IsNullOrEmpty(url)) { return("?" + param.ToUrl()); } var wen_index = url.IndexOf('?'); if (wen_index < 0) { return(url + "?" + param.ToUrl()); } if (wen_index + 1 >= url.Length) { return(url + param.ToUrl()); } var path = url.Substring(0, wen_index); var qstr = url.Substring(wen_index + 1); var q_p = WebParamData.FromUrl(qstr); if (isCover) { //用合并参数覆盖原url参数 foreach (var r in param) { q_p[r.Key] = r.Value; } return($"{path}?{q_p.ToUrl()}"); } else { //用原url参数覆盖合并参数 foreach (var r in q_p) { param[r.Key] = r.Value; } return($"{path}?{param.ToUrl()}"); } }
//数据反转换 /// <summary> /// /// </summary> /// <param name="xml"></param> /// <returns></returns> public static WebParamData FromXml(string xml) { var m_values = new WebParamData(); try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点<xml> XmlNodeList nodes = xmlNode.ChildNodes; foreach (XmlNode xn in nodes) { XmlElement xe = (XmlElement)xn; m_values[xe.Name] = xe.InnerText; } return(m_values); } catch //(Exception ex) { return(m_values); } }
/// <summary> /// /// </summary> /// <param name="url"></param> /// <returns></returns> public static WebParamData FromUrl(string url) { var param = new WebParamData(); try { int l = string.IsNullOrEmpty(url) ? 0 : url.Length; int i = 0; unsafe { fixed(char *s = url) { while (i < l) { int si = i; int ti = -1; while (i < l) { char ch = s[i]; if (ch == '=') { if (ti < 0) { ti = i; } } else if (ch == '&') { break; } i++; } string name = null; string value = null; if (ti >= 0) { name = url.Substring(si, ti - si); value = url.Substring(ti + 1, i - ti - 1); } else { value = url.Substring(si, i - si); i++; continue; } if (!string.IsNullOrEmpty(name) && !(string.IsNullOrEmpty(value))) { param.Add(name, WebHelper.UrlDecode(value)); } i++; } } } return(param); } catch (System.Exception) { return(param); } }
private static bool TryParsePathInfo(StringReader reader, HttpRequestMatch temp_param, StringBuilder temp_sb) { temp_sb.Length = 0; int ch; //开始解析请求头谓词 while (true) { ch = reader.Read(); //回车符继续 if (ch == '\r') { continue; } //遇到意外的换行符或者文档末尾,结束解析 if (ch == -1 || ch == '\n') { return(false); } //遇到空格结束 if (ch == ' ') { break; } temp_sb.Append((char)ch); } temp_param.Method = temp_sb.ToString(); bool hasquerystring = false; //开始解析请求头路径 temp_sb.Length = 0; while (true) { ch = reader.Read(); //回车符继续 if (ch == '\r') { continue; } //遇到意外的换行符或者文档末尾,结束解析 if (ch == -1 || ch == '\n') { return(false); } //遇到空格结束 if (ch == ' ') { break; } if (ch == '?') { //路径中包含查询字符串 hasquerystring = true; break; } temp_sb.Append((char)ch); } temp_param.Path = temp_sb.ToString(); if (hasquerystring) { //路径中包含查询字符串,解析查询参数 temp_sb.Length = 0; while (true) { ch = reader.Read(); //回车符继续 if (ch == '\r') { continue; } //遇到意外的换行符或者文档末尾,结束解析 if (ch == -1 || ch == '\n') { return(false); } //遇到空格结束 if (ch == ' ') { break; } temp_sb.Append((char)ch); } //解析查询字符串信息 temp_param.QueryString = WebParamData.FromUrl(temp_sb.ToString()); } //解析请求头HTTP版本信息 temp_sb.Length = 0; while (true) { ch = reader.Read(); //回车符继续 if (ch == '\r') { continue; } //遇到意外字符 if (ch == ' ') { return(false); } //换行符结束 if (ch == '\n') { break; } temp_sb.Append((char)ch); } temp_param.HttpVersion = temp_sb.ToString(); return(true); }
private static bool TryParseHeaderInfo(StringReader reader, HttpRequestMatch temp_param, StringBuilder temp_sb) { //读取的字符缓存 int ch, linecount = 0; //提供请求头数据缓存 var webParam = new WebParamData(); temp_param.Header = webParam; string key, value; //开始读取请求头 while (true) { ch = reader.Read(); //遇到文档末尾结束 if (ch == -1) { return(true); } //遇到回车符继续 if (ch == '\r') { continue; } //遇到换行符 if (ch == '\n') { linecount++; if (linecount >= 2) { //遇到空行结束 return(true); } continue; } //换行符计数清零 linecount = 0; #region 解析key temp_sb.Length = 0; while (true) { temp_sb.Append((char)ch); ch = reader.Read(); //意外遇到文档末尾结束 if (ch == -1 || ch == '\n') { return(false); } //遇到回车符继续 if (ch == '\r') { continue; } if (ch == ':') { //遇到分隔符,检查下一个字符是否是空格 ch = reader.Peek(); if (ch != ' ') { //不是空格继续 continue; } //是空格,获取键值结束,推进读取游标 reader.Read(); key = temp_sb.ToString(); break; } } #endregion value = reader.ReadLine(); linecount = 1; webParam[key] = value; continue; } }