/// <summary>
        /// 获取文章评论。
        /// </summary>
        /// <param name="articleId">文章 Id。</param>
        /// <param name="pageIndex">第几页,从 1 开始。</param>
        /// <param name="pageSize">每页条数。</param>
        /// <returns>评论列表。</returns>
        /// <exception cref="ArgumentOutOfRangeException">文章 Id 错误。</exception>
        /// <exception cref="ArgumentOutOfRangeException">评论页数错误。</exception>
        /// <exception cref="ArgumentOutOfRangeException">评论条数错误。</exception>
        public static async Task <IEnumerable <ArticleComment> > CommentAsync(int articleId, int pageIndex, int pageSize)
        {
            if (articleId < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(articleId));
            }
            if (pageIndex < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(pageIndex));
            }
            if (pageSize < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(pageSize));
            }

            var url = string.Format(CultureInfo.InvariantCulture, CommentUrlTemplate, articleId, pageIndex, pageSize);

            url = url.WithCache();
            var uri     = new Uri(url, UriKind.Absolute);
            var request = WebRequest.Create(uri);

            using (var response = await request.GetResponseAsync())
            {
                var document = XDocument.Load(response.GetResponseStream());
                var comments = new List <ArticleComment>(CommentHelper.Deserialize <ArticleComment>(document));
                for (var i = 0; i < comments.Count; i++)
                {
                    comments[i].ArticleId = articleId;
                }
                return(comments);
            }
        }
Exemple #2
0
        /// <summary>
        /// 获取新闻评论。
        /// </summary>
        /// <param name="newsId">新闻 Id。</param>
        /// <param name="pageIndex">第几页,从 1 开始。</param>
        /// <param name="pageSize">每页条数。</param>
        /// <returns>评论。</returns>
        /// <exception cref="ArgumentOutOfRangeException">新闻 Id 错误。</exception>
        /// <exception cref="ArgumentOutOfRangeException">评论页数错误。</exception>
        /// <exception cref="ArgumentOutOfRangeException">评论条数错误。</exception>
        public static async Task <IEnumerable <NewsComment> > CommentAsync(int newsId, int pageIndex, int pageSize)
        {
            if (newsId < MinNewsId)
            {
                throw new ArgumentOutOfRangeException(nameof(newsId));
            }
            if (pageIndex < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(pageIndex));
            }
            if (pageSize < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(pageSize));
            }

            try
            {
                var forCheck = checked (pageIndex * pageSize);
            }
            catch (OverflowException exception)
            {
                throw new ArgumentOutOfRangeException("超出范围。", exception);
            }

            var url = string.Format(CultureInfo.InvariantCulture, CommentUrlTemplate, newsId, pageIndex, pageSize);

            url = url.WithCache();
            var uri     = new Uri(url, UriKind.Absolute);
            var request = WebRequest.Create(uri);

            using (var response = await request.GetResponseAsync())
            {
                var document = XDocument.Load(response.GetResponseStream());
                var comments = new List <NewsComment>(CommentHelper.Deserialize <NewsComment>(document));
                for (var i = 0; i < comments.Count; i++)
                {
                    comments[i].NewsId = newsId;
                }
                return(comments);
            }
        }