title: Error: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse
body: <p>I try to upload a file to sharepoint using HttpWebRequest post method. But I get the error like the title show above. I have go through some similer questions but the solutions do not solve my problem.</p>
  <p><a href="https://stackoverflow.com/questions/18596788/you-must-write-contentlength-bytes-to-the-request-stream-before-calling-beging">You must write ContentLength bytes to the request stream before calling [Begin]GetResponse</a>
  <a href="https://stackoverflow.com/questions/31297612/system-net-protocolviolationexception-you-must-write-contentlength-bytes-to-the">System.Net.ProtocolViolationException: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse</a></p>
  <p>Here is my code.</p>
  <pre><code>public static string UploadFile(string filePath, string siteurl, string fileName, string formDigest, string token)
          {
              byte[] fileArray = File.ReadAllBytes(filePath);
              bool status = false;
              string result = string.Empty;
              //Url to upload file
              string resourceUrl = siteurl + &quot;/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents')/Files/add(url='&quot; + fileName + &quot;',overwrite=true)&quot;;
              HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(resourceUrl);
              wreq.UseDefaultCredentials = false;
              //credential who has edit access on document library
              NetworkCredential credentials = new System.Net.NetworkCredential(&quot;username&quot;, &quot;password&quot;, &quot;domain&quot;);
              wreq.Credentials = credentials;
  
              //Get formdigest value from site
  
              wreq.Headers.Add(&quot;X-RequestDigest&quot;, formDigest);
              wreq.Headers.Add(&quot;Authorization&quot;, token);
              wreq.Method = WebRequestMethods.Http.Post;
              wreq.ContentType = &quot;application/x-www-form-urlencoded&quot;;
              wreq.Timeout = 1000000; //timeout should be large in order to upload file which are of large size
              wreq.Accept = &quot;application/json; odata=verbose&quot;;
              wreq.ContentLength = fileArray.Length;
              try
              {
                  using (System.IO.Stream requestStream = wreq.GetRequestStream())
                  {
                      requestStream.Write(fileArray, 0, fileArray.Length);
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
              }
  
              try
              {
                  HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();
                  using (StreamReader reader = new StreamReader(wresp.GetResponseStream()))
                  {
                      result = reader.ReadToEnd();
                      status = true;
                      return result;
                  }
              }
              catch (Exception ex)
              {
                  return ex.Message;
                  throw;
              }
          }
  </code></pre>