0 votes
by (120 points)
edited by

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

// Get the response from the JetLink
HttpResponse webResponse = (HttpResponse)webRequest.GetResponse();

// Get the response from the JetLink
HttpResponse webResponse = (HttpResponse)webRequest.GetResponse();
Applies to: Rebex HTTPS

2 Answers

0 votes
by (145k points)
edited by

Update: Rebex HTTPS 2017 R6.2 supports AllowWriteStreamBuffering and SendChunked.

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
by (145k 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.

Hi Lukas,
I would like to have this hotfix in order to avoid OutOfMemoryException.
Can you send it to me?
Thank you so much

Best regards

by (145k points)
Hi Mauro, I have just sent a link to the hotfix to your e-mail address.
by (145k points)
We have released a new version of Rebex HTTPS with support for AllowWriteStreamBuffering and SendChunked properties: