I’m posting a file with HttpWebRequest, along with a header and footer. The header (ca. 0.5K) and the actual file seem to write fine, but with large files (ca. 15MB), the footer (which is like 29 bytes) never seems to write.
using (Stream requestStream = request.GetRequestStream()) {
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
byte[] buffer = new byte[Math.Min(4096L, fileSize)];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) {
requestStream.Write(buffer, 0, bytesRead);
}
// next line never completes
requestStream.Write(postFooterBytes, 0, postFooterBytes.Length);
// code below is never reached
Console.WriteLine("Why do I never see this message in the console?");
}
Any thoughts?
ETA: Tried flushing the stream before the last Write(), on the off chance it would help, but to no effect.
Edited again: Added using() to clarify that I’m not a complete idiot. Note also BTW that this is inside another using() block for fileStream.
Solved: Turned off
AllowWriteStreamBufferingon theHttpWebRequest. Looks like when it’s on, whateverWrite()call writes the last byte, it doesn’t return till the internal buffer’s cleared. So the lastWrite()was eventually competing, just not till I ran out of patience.And since what I was originally trying to do was determine progress, turning off buffering makes things clearer anyway.