Пример #1
0
        public async Task <ActionResult> Index()
        {
            List <StoryModel>   storyList  = new List <StoryModel>();
            HttpClient          httpClient = myAPI.Initial();
            HttpResponseMessage response   = await httpClient.GetAsync("topstories.json?print=pretty");

            if (response.IsSuccessStatusCode)
            {
                //store the response as a string, clean it up, and make an array from it.
                string result = response.Content.ReadAsStringAsync().Result.ToString();
                var    chars  = new string[] { "]", "[", "\"" };
                foreach (var c in chars)
                {
                    result = result.Replace(c, string.Empty);
                }
                string trimmedResult = result.Trim();
                int[]  storyIDs      = Array.ConvertAll(trimmedResult.Split(','), int.Parse);
                //reduced the result set due to azure timing out.
                Array.Resize(ref storyIDs, storyIDs.Length - 400);

                //it puts the reponse in the list and does what it's told or else it gets the loop again.
                foreach (int id in storyIDs)
                {
                    HttpResponseMessage newResponse = await httpClient.GetAsync("item/" + id + ".json?print=pretty");

                    if (newResponse.IsSuccessStatusCode)
                    {
                        var storyResult = newResponse.Content.ReadAsStringAsync().Result;
                        storyList.Add(JsonConvert.DeserializeObject <StoryModel>(storyResult));
                    }
                }
            }
            return(View(storyList));
        }