title: "The remote server returned an error: (401) Unauthorized" error when trying to upload a file
body: <p>I found a lot of question about (401) Unauthorized error, but none of them explained to me how to diagnose the issue.</p>
  
  <p>I created a new MVC ASP.net app to learn how to upload a file to a sharepoint folder. As of  right now, I can't seem to be able to copy a file from
  C:\testfolder\file.txt to 
  C:\testfolder\UploadedFiles</p>
  
  <p>Here is the method that I made where I send in the source file path and the target folder:</p>
  
  <pre><code> //Flag to indicate whether file was uploaded successfuly or not
          bool isUploaded = true;
          try
          {
              // Create a PUT Web request to upload the file.
              WebRequest request = WebRequest.Create(targetDocumentLibraryPath);
  
              //Set credentials of the current security context
              request.PreAuthenticate = true;
              request.UseDefaultCredentials = true;
              ICredentials credentials = new NetworkCredential( "Username", "password", "Domain"); //I used my username and password here
              request.Credentials = credentials;
              //request.Credentials = CredentialCache.DefaultCredentials;
              request.Method = "PUT";
  
              // Create buffer to transfer file
              byte[] fileBuffer = new byte[1024];
  
              // Write the contents of the local file to the request stream.
              using (Stream stream = request.GetRequestStream())
              {
                  //Load the content from local file to stream
                  using (FileStream fsWorkbook = File.Open(sourceFilePath, FileMode.Open, FileAccess.Read))
                  {
                      //Get the start point
                      int startBuffer = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length);
                      for (int i = startBuffer; i &gt; 0; i = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length))
                      {
                          stream.Write(fileBuffer, 0, i);
                      }
  
                  }
              }
  
              // Perform the PUT request
              WebResponse response = request.GetResponse();
  
              //Close response
              response.Close();
          }
          catch (Exception ex)
          {
              //Set the flag to indiacte failure in uploading
              isUploaded = false; //The remote server returned an error: (401) Unauthorized.
          }
  
          //Return the final upload status
          return isUploaded; 
      }
  </code></pre>
  
  <p>I tried the following:</p>
  
  <ul>
  <li>Changing the permission of the folders to be able to write/read from them for all users.</li>
  <li>Use other folder locations (SharePoint, local drive, network drive) to upload the file to.</li>
  </ul>
  
  <p>Any idea on how to get this problem to work? Any prospective on how to debug the problem is also appreciated.</p>
  
  <p>Update: It is probably being my account is set as the app pool account in IIS. I am still not sure what the issue is.</p>