I'm wanting to track the state of my upload request, something like :
- Upload packet
- Update UI with progress
- Upload packet
- Update UI with progress
- Upload packet
- Update UI with progress
- etc etc
So I tried the following code (gathered from examples found online).
void SendData()
{
byte[] bytes = Encoding.UTF8.GetBytes(GetContent());
HttpWebRequest webRequest = WebRequest.Create(GetURL()) as HttpWebRequest;
webRequest.Method = "PUT";
webRequest.ContentType = @"application/x-www-form-urlencoded";
webRequest.ContentLength = bytes.Length;
// this doesn't seem to help
webRequest.AllowWriteStreamBuffering = false;
using (Stream reqStream = webRequest.GetRequestStream())
{
int bytesUploaded = 0;
while (bytesUploaded != bytes.Length)
{
int packetSize = Math.Min(PACKETSIZE, bytes.Length - bytesUploaded);
reqStream.Write(bytes /*buffer*/,
bytesUploaded /*Offset*/,
packetSize /*Count*/);
// this doesn't seem to help
reqStream.Flush();
bytesUploaded += packetSize;
UI.SetProgress(bytesUploaded / bytes.Length,
true /*Dispatch?*/);
}
}
WebResponse webResponse = webRequest.GetResponse(); //stalls here while it uploads
}
but it isn't working as expected. It isn't uploading the bytes after the flush, but instead running through the multiple iterations of the while loop, and then uploading at the GetResponse() stage. I know its not uploading within the while loop because no matter how large I set the byte array, the time taken to go through the loop barely changes; yet the time at the GetResponse() stage increase proportionately.
Resulting in the progress going from 0-100% in the blink of an eye, and then staying at 100% for the time it takes to actually send the data.