Ejemplo n.º 1
0
        /// <summary>
        /// 获取产品价格
        /// </summary>
        /// <param name="jdNum">jd产品编号</param>
        /// <returns></returns>
        public static decimal GetPriceFromCart(string jdNum)
        {
            const string addcart = "http://cart.360buy.com/cart/addSkuToCartAsync.action?pid={0}&ptype=1&pcount={1}&rd={2}";
            const string cart = "http://cart.360buy.com/cart/getCurrentCartNew.action?rd={0}";
            decimal price = 0;
            int count = 0;
            while ((price == 0 || price == 99M) && count < 2)
            {
                count++;
                try
                {
                    using (
                        var http = new HttpHelper(String.Format(addcart, jdNum, 1, Utils.GetJsonTime()), Encoding.UTF8))
                    {
                        http.GetCookie();
                        http.SetUrl(String.Format(cart, Utils.GetJsonTime()));
                        var html = http.GetHtml();
                        string sprice = RegexHelper.Match(html, "\"finalPrice\":\"([^\"]+)\"");
                        price = Convert.ToDecimal(sprice.Replace(",", ""));
                    }
                }
                catch
                {
                    price = 0;
                }
            }

            #region 旧方式

            //string url = String.Format(cart, (new Random().NextDouble()));
            //int count = 0;
            //while ((price == 0 || price == 99M) && count < 2)
            //{
            //    count++;
            //    try
            //    {
            //        string doc = HtmlCls.GetHtmlByUrl(url,
            //                                          "cart-main=\"{&spg&:{&ps&:[{&i&:" + jdNum +
            //                                          "$&n&:1$&at&:0$&ct&:1}]}$&y&:{}$&by&:false$&rs&:[" + jdNum +
            //                                          "]$&tm&:&" + Utils.GetTimeNow() +"&$&st&:&g&}\"", Encoding.UTF8, _useProxy);
            //        string sprice = RegexHelper.Match(doc, "\"finalPrice\":\"([^\"]+)\"");
            //        price = Convert.ToDecimal(sprice.Replace(",", ""));
            //    }
            //    catch (Exception)
            //    {
            //        price = 0;
            //    }
            //}

            #endregion

            return price;
        }