I am trying to use HttpRequest to stream large files to a web api from a limited memory Windows CE device. The exception is thrown while writing to the request stream. The same problem is not encountered when using HttpWebRequest. I am attempting to use HttpRequest for its TLS support.
Is HttpRequest buffering the content stream? Is there any way to mimic the functionality of HttpWebRequest's AllowWriteStreamBuffering=false or SendChunked=true?
The code is roughly as follows:
string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
var webRequest = DdfWebRequest.Create(postUrl);
webRequest.ContentType = "multipart/form-data; boundary=" + formDataBoundary;
webRequest.Method = "POST";
// Determine the ContentLength before opening the request stream
webRequest.ContentLength = DetermineStreamSize(reportFiles);
// Write form and file data to request stream
using (Stream requestStream = webRequest.GetRequestStream())
{
string fd;
foreach (string reportFile in reportFiles)
{
fd = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\n\r\n", formDataBoundary,
reportId.ToString(), Path.GetFileName(reportFile));
requestStream.Write(Encoding.UTF8.GetBytes(fd), 0, Encoding.UTF8.GetByteCount(fd));
// Get the file content
FileStream fs = new FileStream(reportFile, FileMode.Open, FileAccess.Read);
byte[] data = new byte[2048];
int k = 0;
for (int i = 0; i < fs.Length; i+=k)
{
k = fs.Read(data, 0, data.Length);
if (k > 0)
{
requestStream.Write(data, 0, k);
}
}
fs.Close();
requestStream.Write(Encoding.UTF8.GetBytes("\r\n"), 0, 2);
}
// Add last boundary
string footer = "\r\n--" + formDataBoundary + "--\r\n";
requestStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer));
requestStream.Flush();
requestStream.Close();
}
// Get the response from the JetLink
HttpResponse webResponse = (HttpResponse)webRequest.GetResponse();
// Get the response from the JetLink
HttpResponse webResponse = (HttpResponse)webRequest.GetResponse();