示例#1
0
        public NewsData GetQuestion(string Query, string fromDate)
        {
            NewsData result = new NewsData();

            try
            {
                StringBuilder url = new StringBuilder(255);
                url.Append("http://newsapi.org/v2/everything?");
                url.Append($"q={Query}");
                url.Append($"&from={fromDate}");
                url.Append("&sortBy=popularity");
                url.Append($"&apiKey={this.ApiKey}");

                using (var wc = new WebClient())
                {
                    wc.Encoding = Encoding.UTF8;
                    string tmp = wc.DownloadString(url.ToString());
                    if (!string.IsNullOrWhiteSpace(tmp))
                    {
                        result = this.Deserialize <NewsData>(tmp);
                    }
                }
            }
            catch (Exception ex)
            {
                if (this.Logger != null)
                {
                    this.Logger.Error(ex);
                }
            }

            return(result);
        }
示例#2
0
        public NewsData GetHeadlines(string Country)
        {
            NewsData result = new NewsData();

            try
            {
                StringBuilder url = new StringBuilder(255);
                url.Append("http://newsapi.org/v2/top-headlines?");
                url.Append($"country={Country}");
                url.Append($"&apiKey={this.ApiKey}");

                using (var wc = new WebClient())
                {
                    wc.Encoding = Encoding.UTF8;
                    string tmp = wc.DownloadString(url.ToString());
                    if (!string.IsNullOrWhiteSpace(tmp))
                    {
                        if (this.Logger != null)
                        {
                            this.Logger.Debug(tmp);
                        }
                        result = this.Deserialize <NewsData>(tmp);
                    }
                }
            }
            catch (Exception ex)
            {
                if (this.Logger != null)
                {
                    this.Logger.Error(ex);
                }
            }

            return(result);
        }