text: Q:
  
  Error while reading response from HttpWebRequest
  
  I am trying to send contents of 1GB text file over the network. I modified the  suggested code  for basic authentication and kept it as follows :
          WRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
  
          WRequest.Credentials = Credentials;
          WRequest.PreAuthenticate = true;
  
          WRequest.ContentType = "text/plain"; 
          WRequest.Method = "POST";
          WRequest.AllowWriteStreamBuffering = false;
          WRequest.Timeout = 10000;
  
          FileStream ReadIn = new FileStream(filename, FileMode.Open, FileAccess.Read);
          ReadIn.Seek(0, SeekOrigin.Begin); 
          WRequest.ContentLength = ReadIn.Length; 
          Byte[] FileData = new Byte[ReadIn.Length]; 
          int DataRead = 0;
          Stream tempStream = WRequest.GetRequestStream();
          do
          {
              DataRead = ReadIn.Read(FileData, 0, 2048);
              if (DataRead > 0) 
              {
                  tempStream.Write(FileData, 0, DataRead);
                  Array.Clear(FileData, 0, 2048); 
              }
          } while (DataRead > 0);
  
          // The response
          WResponse = (HttpWebResponse)WRequest.GetResponse();
  
  However, now it gives me System.Net.ProtocolViolationException error : "You must write ContentLength bytes to the request stream before calling [Begin]GetResponse". I checked HttpWebRequest.BeginGetRequestResponse ... and found from debugging that the contentlength for WRequest is not -1. What else could be going wrong ? How should I get the response ?
  Update :
  The code which worked for small files is as followed :
          WebRequest request = WebRequest.Create(url);
          request.Method = "POST";
          request.Credentials = Credentials;
  
          using (StreamReader reader = new StreamReader(filename))
          {
                  postData = reader.ReadToEnd();
          }
  
          byte[] byteArray = Encoding.UTF8.GetBytes(postData);
          request.ContentType = "text/plain";
          request.ContentLength = byteArray.Length;
  
          Stream dataStream = request.GetRequestStream();
          dataStream.Write(byteArray, 0, byteArray.Length);
          dataStream.Close();
  
          // The response
          WebResponse response = request.GetResponse();
          Console.WriteLine(((HttpWebResponse)response).StatusDescription);
  
          dataStream = response.GetResponseStream();
  
          using (StreamReader reader = new StreamReader(dataStream))
          {
               responseFromServer = reader.ReadToEnd();
          }
  
          dataStream.Close();
          response.Close();
  
  A:
  
  The article you referenced says 
  
  If the Microsoft Internet Information Services (IIS) Web server is configured to use Basic authentication, and you must set the HttpWebRequest.AllowWriteStreamBuffering property to false, you must send a HEAD request to pre-authenticate the connection before you send the POST or PUT request. 
  
  EDIT - now with more clarification! 
  To restate the article, if you want to send a large file to a destination which requires basic authentication, you'll need to issue two separate requests. The key here is that you are setting PreAuthenticate = true. Read the statement literally -- by setting the property to true, you are saying that you will authenticate any requests that you make before you actually attempt them! The framework doesn't know how you want to accomplish this pre-authentication, so you need to perform that action yourself, by sending a HEAD request to the destination. Think of the HEAD HTTP method as being a prologue to the actual request - it describes (or requests information about) a particular resource. 
  So the process goes like this:
  
  Make a HEAD request to http://someurl/aresource containing the credentials you want to use when making future requests from this client to that server for the listed resource
  The server will respond (ideally) with "OK - you may proceed. You're authenticated"
  The server immediately regrets its' decision to allow the operation as it finds itself saving a very large file :-)
  
  I don't see you making that HEAD request anywhere in the code you posted - if it's not already there, add this at the beginning of your code (snipped from the sample article ref in OP):
  //preAuth the request
  // You can add logic so that you only pre-authenticate the very first request.
  // You should not have to pre-authenticate each request.
  WRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
  // Set the username and the password.
  WRequest.Credentials = new NetworkCredential(user, password);
  WRequest.PreAuthenticate = true;
  WRequest.UserAgent = "Upload Test";
  WRequest.Method = "HEAD";
  WRequest.Timeout = 10000;
  WResponse = (HttpWebResponse)WRequest.GetResponse(); 
  WResponse.Close();
  // Make the real request.
  
  