I'm sending an HTTP PUT request from a WinForms application and I'd like to send a slow trickle of PUT data to the page that will write messages into a database as the PUT data arrives. I'm using WebRequest and I've set SendChunked to true, but it only seems to send a chunk after 8KB of data have been written to the request stream.
Even worse, the web page seems to stop receiving after about 42KB and the sender throws a WebException after about 77KB with the message, "The request was aborted: The request was canceled."
I'm actually sending very small amounts of data in each message, so if I could convince WebRequest to just send a small chunk containing each message, I'd be fine.
Here's what I'm experimenting with so far:
var request =
(HttpWebRequest)WebRequest.Create("http://localhost/test.php");
request.Method = "PUT";
request.Timeout = 300 * 1000;
request.SendChunked = true;
request.AllowWriteStreamBuffering = false;
request.ContentType = "application/octet-stream";
using (var post = new StreamWriter(request.GetRequestStream()))
{
post.AutoFlush = true;
for (int i = 0; i < 100; i++)
{
if (i > 0)
{
// force flushing previous chunk
post.Write(new String(' ', 1048));
Thread.Sleep(2 * 1000);
}
Console.Out.WriteLine("Requesting {0} at {1}.", i, DateTime.Now);
string chunk = i.ToString();
post.WriteLine(i);
}
}
I'm writing 1KB of whitespace after each message to try and force the WebRequest to send a chunk sooner.