.net 接收post 的参数 加载xml

  /// <summary>
    /// SimDataLimiHandler 的摘要说明  推送
    /// </summary>
    public class SimDataLimiHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            try
            {
                if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")
                {
                    using (Stream stream = HttpContext.Current.Request.InputStream)
                    {
                        //Byte[] postBytes = new Byte[stream.Length];
                        //stream.Read(postBytes, 0, (Int32)stream.Length);
                        //string postString = Encoding.UTF8.GetString(postBytes);


                        try
                        {
                            StreamReader reader = new StreamReader(stream);
                            string jsonStr = reader.ReadToEnd();
                            jsonStr = System.Web.HttpUtility.UrlDecode(jsonStr);//url解码
                            SortedList prams = Param(jsonStr);
                            for (int i = 0; i < prams.Count; i++)
                            {
                                Log4netHelper.Debug("解析", "参数 " + prams.GetKey(i) + "" + prams.GetByIndex(i));
                            }
                            //声明一个XMLDoc文档对象,LOAD()xml字符串  
                            XmlDocument doc = new XmlDocument();
                            doc.LoadXml(prams.GetByIndex(0).ToString());
                            //得到XML文档根节点  
                            XmlElement root = doc.DocumentElement;
                            XmlNodeList list = root.ChildNodes;
                            foreach (XmlNode xno in list)
                            {
                                Console.WriteLine(" 参数 " + xno.Name + "" + xno.InnerText);

                            }
                        }
                        catch (Exception ex)
                        {
                            Log4netHelper.Error("SimDataLimiHandler 数据解析出错", "接收推送", ex);

                        }

                    }


                }
            }
            catch (Exception ex)
            {
                string eventId = context.Request.Form["eventId"];
                string data = context.Request.Form["data"];
                Log4netHelper.Error("SimDataLimiHandler联通推送调用出错", "eventId:" + eventId + "data:" + data, ex);
            }

        }



        private SortedList Param(string POSTStr)
        {

            SortedList SortList = new SortedList();
            int index = POSTStr.IndexOf("&");
            string[] Arr = { };
            if (index != -1) //参数传递不只一项
            {
                Arr = POSTStr.Split('&');
                for (int i = 0; i < Arr.Length; i++)
                {
                    int equalindex = Arr[i].IndexOf('=');
                    string paramN = Arr[i].Substring(0, equalindex);
                    string paramV = Arr[i].Substring(equalindex + 1);
                    if (!SortList.ContainsKey(paramN)) //避免用户传递相同参数
                    { SortList.Add(paramN, paramV); }
                    else //如果有相同的,一直删除取最后一个值为准
                    { SortList.Remove(paramN); SortList.Add(paramN, paramV); }
                }
            }
            else //参数少于或等于1项
            {
                int equalindex = POSTStr.IndexOf('=');
                if (equalindex != -1)
                { //参数是1项
                    string paramN = POSTStr.Substring(0, equalindex);
                    string paramV = POSTStr.Substring(equalindex + 1);
                    SortList.Add(paramN, paramV);
                }
                else //没有传递参数过来
                { SortList = null; }
            }
            return SortList;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

 

posted @ 2016-12-15 17:58  文艺流浪汉  阅读(371)  评论(0编辑  收藏  举报