title: An error occurred while sending the request. System.Net.Http
body: <p>I am loading 20 items for a list. The informations I need are from Instagram. So I send some requests but I get random errors sometimes. Not every time. Do you know what could cause the random error? Maybe the asnc task to request all the 20 items at a time?</p>
  
  <p>I use Visual Studio 2015, Windows 10, Universal Windows App.</p>
  
  <p><strong>Error Code:</strong></p>
  
  <pre><code>An error occurred while sending the request.
  
     at System.Net.Http.HttpClientHandler.&lt;SendAsync&gt;d__1.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at Instalike.InstagramAPI.&lt;SendAsync&gt;d__4`1.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at Instalike.InstagramAPI.&lt;getProfilePicURLAndNameByID&gt;d__12.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at Instalike.MainViewModel.&lt;&gt;c__DisplayClass50_1.&lt;&lt;LoadChartsData&gt;b__0&gt;d.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Instalike.MainViewModel.&lt;LoadChartsData&gt;d__50.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Instalike.ChartsList.&lt;ChartsList_Loaded&gt;d__6.MoveNext()
  </code></pre>
  
  <p><strong>This is the code:</strong></p>
  
  <p>ChartsList.xaml.cs</p>
  
  <pre><code>public ChartsList()
          {
              this.InitializeComponent();
  
              //Frame mainFrame = Window.Current.Content as Frame;
              //mainFrame.ContentTransitions = null;
              //mainFrame.ContentTransitions = new TransitionCollection { new PaneThemeTransition { Edge = EdgeTransitionLocation.Left } };
              DataContext = App.ViewModel;
          }
  
          private async void ChartsList_Loaded(object sender, RoutedEventArgs e)
          {
              if (await MainPage.ReadToken() != null)
              {
                  if (apiClient2 == null)
                  {
                      await LoadData();
                  }
                  if (!App.ViewModel.IsChartsDataLoaded)
                  {
                      setProgressIndicator(true);
                      bool successfull = false;
                      try
                      {
                          await App.ViewModel.LoadChartsData();
                          successfull = true;
                      }
                      catch (Exception)  // Here the exception gets catched!
                      {
                          successfull = false;
                          //throw;
                      }
                      setProgressIndicator(false);
                      if (successfull &amp;&amp; !App.ViewModel.IsChartsDataLoaded)
                      {
                          MainPage.tokenDeleted = false;
                          Frame.Navigate(typeof(Authentication));
                      }
                      else
                      {
  
                      }
                  }
                  else
                  {
  
                  }
              }
              else
              {
                  Frame.Navigate(typeof(Authentication));
              }
          }
  
          /// &lt;summary&gt;
          /// Wird aufgerufen, wenn diese Seite in einem Frame angezeigt werden soll.
          /// &lt;/summary&gt;
          /// &lt;param name="e"&gt;Ereignisdaten, die beschreiben, wie diese Seite erreicht wurde.
          /// Dieser Parameter wird normalerweise zum Konfigurieren der Seite verwendet.&lt;/param&gt;
          protected override void OnNavigatedTo(NavigationEventArgs e)
          {
              base.OnNavigatedTo(e);
          }
  </code></pre>
  
  <p>The LoadChartsData() method:</p>
  
  <pre><code>public async Task LoadChartsData()
          {
             ........
  
              try
              {
                  ChartsViewCache = await table
                      .Take(20)
                      .Skip(0)
                      //.Where(item =&gt; item.likes &gt; 0)
                      .OrderByDescending(item =&gt; item.likes)
                      .ToCollectionAsync();
              }
              catch (MobileServiceInvalidOperationException exc)
              {
                  MessageDialogHelper.Show(exc.Message, ResourceLoaderHelper.getString("msgErrorLoadingChartsTitle"));
              }
              if (ChartsViewCache != null)
              {
                  BadgeHelper badgeHelper = new BadgeHelper();
                  var tasks = new List&lt;Task&gt;();
                  foreach (var item in ChartsViewCache)
                  {
                      var thisTask = Task.Run(async () =&gt;
                      {
                          string[] profileInfos = await apiClient.getProfilePicURLAndNameByID(item.userid); // This one can cause the exception!!!!!
                          item.profilePicURL = profileInfos[0];
                          item.username = profileInfos[1];
                          string[] relationship = await apiClient.getRelationshipByID(item.userid); // But these can cause it too!! Let's focus on the first one.
                          MobileServiceCollection&lt;Badge, Badge&gt; badge = await badgeHelper.readBadge(item.userid);
                          int[] followsAndViews = await getFollowCountAndViewsByID(item.userid);
                          item.followcount = followsAndViews[0];
                          item.views = followsAndViews[1];
  
                         .........
  
                      });
                      tasks.Add(thisTask);
                  }
                  await Task.WhenAll(tasks);
                  ChartsView = ChartsViewCache;
              }
              if (ChartsView != null)
              {
                  NotifyPropertyChanged("ChartsView");
                  this.IsChartsDataLoaded = true;
              }
          }
  </code></pre>
  
  <p>The getProfilePicURLAndNameByID() Method:</p>
  
  <pre><code>public async Task&lt;string[]&gt; getProfilePicURLAndNameByID(string id)
          {
              var request = new HttpRequestMessage(HttpMethod.Get, apiBaseUrl + "/users/" + id + "?access_token=" + apiAccessToken);
              var picResponse = await SendAsync&lt;InstagramResponseProfilePicURLByID&gt;(request);
              string[] result = { "bad", "bad" };
              if (picResponse != null)
              {
                  if (picResponse.data != null)
                  {
                      result[0] = picResponse.data.profile_picture;
                      result[1] = picResponse.data.username;
                  }
              }
              return result;
          }
  </code></pre>
  
  <p>And now the SendAsync() Method:</p>
  
  <pre><code> private async Task&lt;T&gt; SendAsync&lt;T&gt;(HttpRequestMessage request) where T : class
          {
              HttpResponseMessage response;
              response = await _httpClient.SendAsync(request);
  
              //TODO: Error Handling
              if (response.StatusCode == HttpStatusCode.OK)
              {
                  string responseBody = await response.Content.ReadAsStringAsync();
  
                  return JsonConvert.DeserializeObject&lt;T&gt;(responseBody);
              }
              return null;
          }
  </code></pre>