public async static Task <List <NewsInfo> > GetTrendingNewsAsync()
        {
            List <NewsInfo> newsResults = new List <NewsInfo>();
            const string    bingAPIKey  = "123165f3200a4b0c9bb36f4d3087410d";
            string          queryUri    = "https://api.cognitive.microsoft.com/bing/v5.0/news/search";

            HttpClient httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", bingAPIKey);
            httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
            string           bingRawResponse;         //raw response from REST endpoint
            TrendingNewsInfo bingJsonResponse = null; //Deserialized response

            try
            {
                bingRawResponse = await httpClient.GetStringAsync(queryUri);

                bingJsonResponse = JsonConvert.DeserializeObject <TrendingNewsInfo>(bingRawResponse);
            }
            catch (Exception ex)
            {
            }
            newsResults = bingJsonResponse.value.ToList();

            return(newsResults);
        }
示例#2
0
        public async static Task <List <NewsInfo> > GetTrendingNewsAsync()
        {
            List <NewsInfo> newsResults = new List <NewsInfo>();

            const string bingAPIKey = "123165f3200a4b0c9bb36f4d3087410d";
            string       queryUri   = "https://api.cognitive.microsoft.com/bing/v5.0/news/search";

            HttpClient httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", bingAPIKey);
            httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
            string           bingRawResponse;
            TrendingNewsInfo bingJsonResponse = null;

            try
            {
                bingRawResponse = await httpClient.GetStringAsync(queryUri);

                bingJsonResponse = JsonConvert.DeserializeObject <TrendingNewsInfo>(bingRawResponse);
            }
            catch (Exception ex)
            {
            }
            NewsInfo[] newsResult = bingJsonResponse.value;

            if (newsResult == null || newsResult.Length == 0)
            {
                //add code to handle the case where results are null are zero
            }

            return(newsResults);
        }