.net framwork 获取Post和get的参数

public string Order_Back()
        {
    
    
          //调用方法
            SortedDictionary<string, object> valuePairs = GetPostInfo();
            LogHelper.WriteBarcodesLog(string.Format("order_Back:取消订单推送【{1}】推送内容:【{0}】",JsonConvert.SerializeObject(valuePairs), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")), "JianYiBao");
            return API.order_Back(valuePairs["order_id"].ToString(), valuePairs["company_order_id"].ToString());
        }


//获取参数方法
 public SortedDictionary<string, object> GetPostInfo()
        {
    
    
            SortedDictionary<string, object> dir = new SortedDictionary<string, object>();
            var ee = HttpContext.Request;
            string postString = string.Empty;
            using (Stream stream = System.Web.HttpContext.Current.Request.InputStream)
            {
    
    
                Byte[] postBytes = new Byte[stream.Length];

                stream.Read(postBytes, 0, (Int32)stream.Length);

                postString = Encoding.UTF8.GetString(postBytes);

                LogHelper.WriteBarcodesLog(string.Format("消息内容:{0}", postString), "JanYiBao");

                try
                {
    
    
                    dir = GetRequestPost(dir);

                    dir = GetRequestGet(dir);
                }
                catch (Exception ex)
                {
    
    
                    LogHelper.WriteBarcodesLog(string.Format("参数获取异常:{0}", ex.Message), "JanYiBao");
                }
                LogHelper.WriteBarcodesLog(string.Format("消息内容数量:{0}", dir.Count), "JanYiBao");
            }
            return dir;
        }
        public SortedDictionary<string, object> GetRequestPost(SortedDictionary<string, object> pairs)
        {
    
    
            int i = 0;
            SortedDictionary<string, object> sArray = pairs;
            NameValueCollection coll;
            //Load Form variables into NameValueCollection variable.
            coll = Request.Form;

            // Get names of all forms into a string array.
            String[] requestItem = coll.AllKeys;
            if (requestItem.Length > 1)
            {
    
    
                try
                {
    
    
                    for (i = 0; i < requestItem.Length; i++)
                    {
    
    
                        sArray.Add(requestItem[i], Request.Form[requestItem[i]] ?? "");
                    }
                }
                catch (Exception ex)
                {
    
    
                    LogHelper.WriteBarcodesLog(string.Format("GetRequestPost_Error:{0}", ex.Message), "JanYiBao");
                }
            }
            return sArray;
        }

        private SortedDictionary<string, object> GetRequestGet(SortedDictionary<string, object> pairs)
        {
    
    
            int i = 0;
            SortedDictionary<string, object> sArray = pairs;
            NameValueCollection coll;
            //Load Form variables into NameValueCollection variable.
            coll = Request.QueryString;

            // Get names of all forms into a string array.
            String[] requestItem = coll.AllKeys;
            if (requestItem.Length > 1)
            {
    
    
                try
                {
    
    
                    for (i = 0; i < requestItem.Length; i++)
                    {
    
    
                        ///防止参数为null时签名无法通过的问题
                        string paramvalue = Request.QueryString[requestItem[i]] ?? "";
                        if (paramvalue.ToLower().Equals("null"))
                        {
    
    
                            paramvalue = string.Empty;
                        }
                        sArray.Add(requestItem[i], HttpUtility.UrlDecode(paramvalue));
                    }
                }
                catch (Exception ex)
                {
    
    
                    LogHelper.WriteBarcodesLog(string.Format("GetRequestQuerystring_Error:{0}", ex.Message), "JanYiBao");
                }
            }
            return sArray;
        }

猜你喜欢

转载自blog.csdn.net/qq_42455262/article/details/120819612