title: HttpClient error with PostAsync method
body: <p>When making PostAsync call using HttpClient to a 3rd party API. I am seeing this error exactly when I do client.PostAsync. Any idea what could have been causing this?</p>
  
  <p>Code:</p>
  
  <pre><code>public class JobController : AsyncController
  {
      public ActionResult ViewPage()
      {
          return View("~/Views/Pages/Submit.cshtml");
      }
  
      private const string ServiceUrl = "https://api.3points.io/v1/applications/";
  
      [HttpPost]
      public async Task&lt;ActionResult&gt; Submit()
      {
          var client = new HttpClient();
          var formData = new MultipartFormDataContent();
          var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes("abc123"));
          client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encoded);
  
          foreach (var key in Request.Form.AllKeys)
              formData.Add(new StringContent(Request.Form[key]), String.Format("\"{0}\"", key));
  
          foreach (string key in Request.Files.AllKeys)
          {
              var file = Request.Files[key];
              if (file == null || file.ContentLength &lt;= 0) continue;
  
              HttpContent fileStream = new StreamContent(file.InputStream);
              formData.Add(fileStream, key, file.FileName);
          }
  
          var response = await client.PostAsync(ServiceUrl, formData);
  
          var success = "True";
          if (!response.IsSuccessStatusCode) success = "False";
  
          return new JsonResult { Data = success };
      }
  }
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>System.NullReferenceException was unhandled 
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=System.Web
  StackTrace:
     at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext)
     at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
     at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state)
     at System.Web.LegacyAspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state)
     at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task&amp; currentTask)
  --- End of stack trace from previous location where exception was thrown ---
     at System.Threading.Tasks.AwaitTaskContinuation.&lt;ThrowAsyncIfNecessary&gt;b__1(Object s)
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
     at System.Threading.ThreadPoolWorkQueue.Dispatch()
  </code></pre>