title: Cannot close stream until all bytes are written (GoodData API)
body: <p>I've done a bit of searching, and most people seem to hit this when sending larger amounts of data, but I'm not. </p>
  
  <p>I'm making a request to an API with the following:</p>
  
  <pre><code>request.Method = "POST";
  request.ContentType = "application/json";
  request.Accept = "application/json";
  request.Headers.Add("Cookie", "$Version=0; GDCAuthTT=" + TToken + "; $Path=/gdc/account");
  
  //generate request parameters
  ReportRequest.RootObject reportRequest = new ReportRequest.RootObject();
  reportRequest.report_req = new ReportRequest.ReportReq();
  reportRequest.report_req.reportDefinition = ReportLink;
  JavaScriptSerializer serializer = new JavaScriptSerializer();
  byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(serializer.Serialize(reportRequest));
  request.ContentLength = byteArray.Length;
  
  using (var writer = new System.IO.StreamWriter(request.GetRequestStream(), Encoding.UTF8, byteArray.Length))
  {
      writer.Write(byteArray);
  }
  </code></pre>
  
  <p>The exception occurs on the last closing bracket: </p>
  
  <blockquote>
    <p>Cannot close stream until all bytes are written.  Description: An
    unhandled exception occurred during the execution of the current web
    request. Please review the stack trace for more information about the
    error and where it originated in the code. </p>
    
    <p>Exception Details: System.IO.IOException: Cannot close stream until
    all bytes are written.</p>
    
    <p>Source Error: </p>
    
    <p>Line 176:            { Line 177:<br>
    writer.Write(byteArray); Line 178:            } Line 179:<br>
    string responseContent; Line 180:            using (var response =
    request.GetResponse() as System.Net.HttpWebResponse) </p>
    
    <p>Source File: c:\Users\james.billings\Documents\Visual Studio
    2012\Projects\PhoneMonitor\PhoneMonitor\GoodData\DataRetriever.cs<br>
    Line: 178 </p>
    
    <p>Stack Trace: </p>
    
    <p>[IOException: Cannot close stream until all bytes are written.]<br>
    System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean
    aborting) +604</p>
    
    <p>[WebException: The request was aborted: The request was canceled.]<br>
    System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean
    aborting) +6443608<br>
    System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExState
    closeState) +37    System.Net.ConnectStream.Dispose(Boolean disposing)
    +44</p>
  </blockquote>
  
  <p>Any clues? Specifically I'm trying to use the GoodData API call xtab2/executor3 - see <a href="http://docs.gooddata.apiary.io/" rel="nofollow noreferrer">http://docs.gooddata.apiary.io/</a></p>
  
  <p>If I remove the "<code>set ContentLength</code>" I get a 400 Bad Request. If I set to 0 like earlier GET requests (which work fine) I get an error that I tried to write more than the length of the buffer, which makes sense.</p>
  
  <p>Bit stumped!</p>