对一个链接里面未加密的参数进行加密,其中有json对象

代码:

  protected string FormatUrlEncode(string url)
        {
            string result = string.Empty;
            if (url.IndexOf("?") < 0) return result;
            string domain=url.Split(?)[0].ToString();
            string paras = url.Split(?)[1].ToString();
            string[] para = paras.Split(&);
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append(domain+"?");
            foreach (var pa in para)
            {
                string name = pa.Split(=)[0];
                string value = pa.Split(=)[1];
                if (value.Contains ([)&&value.Contains (])&&value.Contains ({)&&value .Contains (}))
                {
                    string content = value.Substring(1, value.Length - 2);
                    List<string> list = new List<string>();
                    //这里不能用逗号,因为对象属性间也是逗号
                    if(content.Contains("},{"))
                    {
                        list = Regex.Split(content, "},{").ToList();
                        list[0] = list[0].Substring(1, list[0].Length - 1);
                        int index = list.Count() - 1;
                        list[index] = list[index].Substring(0, list[index].Length - 1);
                    }
                    else
                    {
                        list.Add(content.Substring(1, content.Length - 2));
                    }
                    strBuilder.Append(name + "=[");
                    foreach (var l in list)
                    {
                        //这里的l对象是去除掉{,}的属性集
                        strBuilder.Append("{");
                        string[] jsons = l.Split(,);
                        foreach (var j in jsons)
                        {
                            //当val是个链接,内部含有冒号时,会少数据
                            string[] vals = j.Split(:);
                            string key = j.Split(:)[0];
                            string val = j.Split(:)[1];
                            //排查http://www.baidu.com
                            if (vals.Length > 2)
                            {
                                val = j.Substring(key.Length+1, j.Length - key.Length-1);
                            }
                            strBuilder.Append(key + ":" + HttpUtility.UrlEncode(val) + ",");
                        }
                        strBuilder = strBuilder.Remove(strBuilder.Length - 1, 1);
                        strBuilder.Append("}");
                        strBuilder.Append(",");
                    }
                    strBuilder = strBuilder.Remove(strBuilder.Length - 1, 1);
                    strBuilder.Append("]&");
                }
                else
                {
                    strBuilder.Append(name + "=" + HttpUtility.UrlEncode(value) + "&");
                }
            }
            strBuilder = strBuilder.Remove(strBuilder.Length - 1, 1);
            return strBuilder.ToString();
        }

链接:

http://www.baidu.com?blabel=&buid=&createTime=2015-01-16 10:02:42&customerAddress=&customerComment=&customerMobile=&customerName=&orderList=[{"agentName":"携程","itemAmount":2,"itemId":3022641,"itemImgUrl":"","itemName":"同程测试资源01","itemOrderDetail":"","itemOrderDetailWap":"","itemOrderTime":"2014-12-10","itemPrice":10000,"itemUrl":"http://piao.ctrip.com/dest/t12.html","itemUrlWap":"http://m.ctrip.com/webapp/ticket/dest/t12.html"}]&orderName= 
同程测试产品&partnerId=Ctrip&payWay=在线支付&srcOrderId=1800000009&status=104&totalAmount=11000&key=baidulvyou_wangyonghui

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。