Cannot close stream until all bytes are written (GoodData API)

前端 未结 3 1406

I\'ve done a bit of searching, and most people seem to hit this when sending larger amounts of data, but I\'m not.

I\'m making a request to an API with the followin

相关标签:
3条回答
  • 2021-01-17 18:28

    Having looked over the docs - System.IO.StreamWriter.Write() - There does not appear to be a method for writing bytes.

    The only method that matches the signature is - StreamWriter.Write(Object). This however calls ToString() on the object and writes the output; Which is not what you want.

    As you are setting an output buffer; the stream is waiting for this buffer to be filled. However, the Object.ToString() will likely not fill this buffer and hence the error.

    Use BinaryWriter, BufferedStream or another that supports byte[] writing.

    0 讨论(0)
  • 2021-01-17 18:31

    For me it was going wrong with a special character (é) in a Json request. For some reason I had to set the ContentLength manually.

    Thanks to the tip on this page I changed my code to the following and for me it works now.

    Old version:

    string content = "{ test: \"olé\" }";
    
    _Request.ContentLength  = content.Length;
    
    using ( var writer = new StreamWriter(_Request.GetRequestStream()) ) 
        writer.Write( content );
    

    New version:

    string content = "{ test: \"olé\" }";
    
    byte[] bytes            = Encoding.UTF8.GetBytes(content);
    _Request.ContentLength  = bytes.Length;
    
    using ( var writer = _Request.GetRequestStream() )
        writer.Write(bytes, 0, bytes.Length);
    
    0 讨论(0)
  • 2021-01-17 18:53

    Do not set request.ContentLength = byteArray.Length; before writing the request stream. The request.ContentLength is set automatically.

    0 讨论(0)
提交回复
热议问题