url参数用&拼接并且按照字母顺序排序方法

 private string urlParamManager(JObject postArray, bool flag)

        {

            ArrayList keys = new ArrayList();

            foreach (JToken child in postArray.Children())

            {

                JProperty property = child as JProperty;

                if (flag)

                {

                    if (!"signMethod".Equals(property.Name) && !"signature".Equals(property.Name))

                    {

                        keys.Add(property.Name);

                    }

                }

                else

                {

                    keys.Add(property.Name);

                }

            }

            keys.Sort();

            StringBuilder sb = new StringBuilder();

            foreach (string key in keys)

            {

                if (flag)

                {

                    if (!string.IsNullOrEmpty(postArray[key].Value<string>()))

                    {

                        if (string.IsNullOrEmpty(sb.ToString()))

                        {

                            sb.Append(key + "=" + postArray[key].Value<string>());

                        }

                        else

                        {

                            sb.Append("&" + key + "=" + postArray[key].Value<string>());

                        }

                    }

                }

                else

                {

                    if (string.IsNullOrEmpty(sb.ToString()))

                    {

                        sb.Append(key + "=" + postArray[key].Value<string>());

                    }

                    else

                    {

                        sb.Append("&" + key + "=" + postArray[key].Value<string>());

                    }

                }

            }

            return sb.ToString();

        }

调用方式:

 JObject param = new JObject();

 param["transaction"] = transaction;

 param["payType"] = payType;

param["userId"] = userId;

string paramStr = this.urlParamManager(param, true);

 

你可能感兴趣的:(url)