對一個連結裡面未加密的參數進行加密,其中有json對象

來源:互聯網
上載者:User

標籤:

代碼:

  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

 

對一個連結裡面未加密的參數進行加密,其中有json對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.