HttpRequest OutOfMemoryException on Windows CE

0 votes
asked Nov 8 by rayo (120 points)
edited Nov 9 by rayo

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();

2 Answers

0 votes
answered Nov 9 by Lukas Pokorny (87,030 points)

Unfortunately, on-the-fly uploads (AllowWriteStreamBuffering=false) and chunked upload mode (SendChunked=true) are not implemented yet. Sorry!

However, these features are currently on top of our list of enhancements and will appear in one of the next releases.

We currently have a partially-finished beta that implements AllowWriteStreamBuffering=false. When we add SendChunked=true as well, I'll send you a link to a beta package to try.

0 votes
answered 6 days ago by Lukas Pokorny (87,030 points)

We have published a hotfix that adds AllowWriteStreamBuffering and SendChunked properties. I have sent a download link to your e-mail address. This will be included in the next release of Rebex HTTPS as well.

...