首页 新闻 赞助 找找看

http请求过来的数据较大怎么高效读取

0
悬赏园豆:50 [已关闭问题] 解决于 2016-03-25 12:47

开发一个第三方充值接口,我请求一个接口,第三方返回全部的商品信息(由于该接口只能30分钟请求一次,我想把所有的数据放到缓存中),商品信息是xml格式的。但由于返回的数据量太大,大概有10000条左右,我这边读取返回的数据的时候,造成服务器错误(503错误)。我经理说建议用http分字节读取的形式,但不是太懂,网上百度了下没有结果。

问下大家有没有好的方式来对http请求过来的数据,进行分字节读取的,或者好的优化方式

我把我写的代码贴出来。

1、模拟http请求的代码

View Code
        /// <summary>
        /// 模拟Http请求
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        String GetHttpRequest(string url)
        {
            if (string.IsNullOrEmpty(url))
                return string.Empty;
            System.Net.HttpWebRequest request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            request.MaximumAutomaticRedirections = 1;
            request.AllowAutoRedirect = false;
            request.ServicePoint.Expect100Continue = false;
            System.IO.StreamReader stream = null;
            string responseValue = string.Empty;
            try
            {

                stream = new System.IO.StreamReader(request.GetResponse().GetResponseStream(), Encoding.GetEncoding("gb2312"),true,20000);
                responseValue = stream.ReadToEnd();
            }
            catch
            {
                responseValue = string.Empty; ;
            }
            finally
            {
                request.GetResponse().GetResponseStream().Close();
                stream.Close();
                stream = null;
            }
            return responseValue;
        }

2、请求第三方接口,返回的结果,存放到缓存中

View Code
        /// <summary>
        /// 查询商品列表(无缓存操作)
        /// 2013.4.24 -by 嘿嘿
        /// </summary>
        /// <param name="categoryname">产品类别名称</param>
        /// <param name="lst">返回全部的商品信息列表</param>
        /// <returns></returns>
        public bool GetProductListClearCache()
        {
            string param = this.GetParam("");
            string url = this._queryProduct + param;
            string result = this.GetHttpRequest(url);
            GameTradingByPublic.PublicMethods.GetLogManager.Debug(result);
            List<ReturnType.GoodsInfo> lst = new List<ReturnType.GoodsInfo>();
            GameTradingByPublic.ICached cached = GameTradingByPublic.CacheManager.GetMemCached();
            if (!string.IsNullOrEmpty(result) && result.IndexOf("ret_code") < 0)
            {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.LoadXml(result);
                System.Xml.XmlNodeList nodelst = doc.GetElementsByTagName("ProductItem");

                for (int i = 0; i < nodelst.Count; i++)
                    lst.Add(this.ConvertObj<ReturnType.GoodsInfo>(nodelst[i]));
            }
            if (lst != null && lst.Count > 0)
            {
                object obj = cached.GetValue("JCardRecharge_Recharge_GetProductList");
                if (obj != null)
                {
                    //清除旧缓存
                    cached.DeleteValue("JCardRecharge_Recharge_GetProductList");
                }
                //增加新缓存
                cached.PutValue("JCardRecharge_Recharge_GetProductList", lst, 24 * 60 * 60);
                return true;
            }
            return false;
        }
keepnode的主页 keepnode | 初学一级 | 园豆:162
提问于:2013-04-25 20:42
< >
分享
其他回答(1)
0

这种最好是通过其他的方式,从对方那边获取数据。而且商品的信息一般还算是稳定的。

收获园豆:10
chenping2008 | 园豆:9836 (大侠五级) | 2013-04-25 21:28

关键问题就在这,这几天,第三方充值接口商品信息不断有调整,造成我们发布商品信息时,第三方商品已经下架了!由于我们这边商品是从缓存读取的,才会想到这个清除缓存,重新加载商品信息到缓存中的方法。请问有没有什么好的解决方法

支持(0) 反对(0) keepnode | 园豆:162 (初学一级) | 2013-04-26 09:42
0

数据量太大的话 就边读边写 不要一次性读取完了再进行写出!

收获园豆:10
红花郎 | 园豆:323 (菜鸟二级) | 2013-04-29 13:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册