获取http三种请求的方式,get,post,流的形式

获取http三种请求的方式,get,post,流的形式

        /// 
        /// get的请求
        /// 
        /// 
        public static Dictionary<string, string> GetParamByGet()
        {
            Dictionary<string, string> sArray = new Dictionary<string, string>();
            NameValueCollection coll;
            coll = HttpContext.Current.Request.QueryString;
            String[] requestItem = coll.AllKeys;
            for (int i = 0; i < requestItem.Length; i++)
            {
                sArray.Add(requestItem[i], HttpContext.Current.Request.QueryString[requestItem[i]]);
            }
            return sArray;
        }

        /// 
        /// post的请求
        /// 
        /// 
        public static Dictionary<string, string> GetParamsPost()
        {
            Dictionary<string, string> sArray = new Dictionary<string, string>();
            NameValueCollection coll = HttpContext.Current.Request.Form;
            String[] requestItem = coll.AllKeys;
            for (int i = 0; i < requestItem.Length; i++)
            {
                if (requestItem[i] == null)
                {
                    continue;
                }
                if (sArray.ContainsKey(requestItem[i]))
                {
                    continue;
                }
                sArray.Add(requestItem[i], HttpContext.Current.Request.Form[requestItem[i]]);
            }
            return sArray;
        }


        /// 
        /// 获取数据流的情况,位置从0开始 application/json的获取情况
        /// 
        /// 
        public static Dictionary<string, object> GetParamsRequestGet(HttpContext context, bool isPositionZero)
        {
            try
            {
                if (isPositionZero) //如果为True的情况则从0开始读取
                {
                    context.Request.InputStream.Position = 0;
                }
                System.IO.Stream s = context.Request.InputStream;
                string builder = Encoding.UTF8.GetString(ReadStream(s));
                s.Flush();
                s.Close();
                s.Dispose();
                QueryDictionary pairs = new QueryDictionary(builder.ToString());
                Dictionary<string, object> paramsDict = new Dictionary<string, object>();
                foreach (KeyValuePair<string, string> keyValuePair in pairs)
                {
                    paramsDict.Add(keyValuePair.Key, keyValuePair.Value);
                }
                return paramsDict;
            }
            catch
            {
                return new Dictionary<string, object>();
            }
        }

你可能感兴趣的:(C#,开发语言,c#)