PRB: HttpWebRequest POST with AllowWriteStreamBuffering = False[.net 1.1, vs2003]

C

Chris Morse

Hi,

This is a strange problem for me, because I know this was working
correctly, but now when I am testing it, it is not working. Nothing
changed in my code, it merely acts now as if I have not set
"HttpWebRequest.AllowWriteStreamBuffering = False" and all data is
sent quickly (ie, buffered locally). The whole point of turning off
the buffering is so that I can provide a progress bar for larger
uploads.

Here is the basic code:

Dim H As HttpWebRequest = HttpWebRequest.Create(szRequestURL)
H.ContentType = "application/octet-stream"
H.Method = "POST"
H.Timeout = nTimeoutSec * 1000
H.AllowWriteStreamBuffering = False
H.ContentLength = FI.Length

Dim S As Stream = H.GetRequestStream()

Dim timeStart As DateTime = DateTime.Now
Dim TS As TimeSpan

Dim nRead As Integer
Dim BUFFER(4096) As Byte
Dim nBytesSent As Integer = 0

Me.ProgressBar1.Minimum = 0
Me.ProgressBar1.Maximum = CInt(FI.Length)
Me.ProgressBar1.Value = 0

Do
nRead = myStreamReader.Read(BUFFER, 0, 4096)
If nRead > 0 Then
S.Write(BUFFER, 0, nRead)
'S.Flush()
nBytesSent += nRead
Me.ProgressBar1.Value = nBytesSent
Application.DoEvents()
Else
Exit Do 'All data sent successfully
End If
Loop

myStreamReader.Close()
myStreamReader = Nothing


I have done google searches and checked MSDN and everything seems to
be correct in the code. Also, I know I had this working before, it
used to send data slowly (my internet connection has a max upload
speed of about 50KB/sec, so it takes a good 20 seconds to upload
1MB). Now a 5MB upload is completed in under 0.03 seconds, and, after
the code above, the code hangs on the following line - while the
uploads is actually completed.

Dim WR As HttpWebResponse = CType(H.GetResponse(),
HttpWebResponse)

There is no authentication or redirections required, and I don't get
any exceptions thrown. It simply acts as if I never issued the
"H.AllowWriteStreamBuffering = False" call.

Any ideas on what could be amiss?

By the way-- This code is running under .NET 1.1 and VS2003.
 
J

Jesse Houwing

* Chris Morse wrote, On 26-1-2010 15:46:
Hi,

This is a strange problem for me, because I know this was working
correctly, but now when I am testing it, it is not working. Nothing
changed in my code, it merely acts now as if I have not set
"HttpWebRequest.AllowWriteStreamBuffering = False" and all data is
sent quickly (ie, buffered locally). The whole point of turning off
the buffering is so that I can provide a progress bar for larger
uploads.

Here is the basic code:

Dim H As HttpWebRequest = HttpWebRequest.Create(szRequestURL)
H.ContentType = "application/octet-stream"
H.Method = "POST"
H.Timeout = nTimeoutSec * 1000
H.AllowWriteStreamBuffering = False
H.ContentLength = FI.Length

Dim S As Stream = H.GetRequestStream()

Dim timeStart As DateTime = DateTime.Now
Dim TS As TimeSpan

Dim nRead As Integer
Dim BUFFER(4096) As Byte
Dim nBytesSent As Integer = 0

Me.ProgressBar1.Minimum = 0
Me.ProgressBar1.Maximum = CInt(FI.Length)
Me.ProgressBar1.Value = 0

Do
nRead = myStreamReader.Read(BUFFER, 0, 4096)
If nRead> 0 Then
S.Write(BUFFER, 0, nRead)
'S.Flush()
nBytesSent += nRead
Me.ProgressBar1.Value = nBytesSent
Application.DoEvents()
Else
Exit Do 'All data sent successfully
End If
Loop

myStreamReader.Close()
myStreamReader = Nothing


I have done google searches and checked MSDN and everything seems to
be correct in the code. Also, I know I had this working before, it
used to send data slowly (my internet connection has a max upload
speed of about 50KB/sec, so it takes a good 20 seconds to upload
1MB). Now a 5MB upload is completed in under 0.03 seconds, and, after
the code above, the code hangs on the following line - while the
uploads is actually completed.

Dim WR As HttpWebResponse = CType(H.GetResponse(),
HttpWebResponse)

There is no authentication or redirections required, and I don't get
any exceptions thrown. It simply acts as if I never issued the
"H.AllowWriteStreamBuffering = False" call.

Any ideas on what could be amiss?

By the way-- This code is running under .NET 1.1 and VS2003.

I think you've interpreted the actual meaning of the property
incorrectly. The documentation states that setting this property to
false, instructs the application to not keep a copy of the data around
after transferring it to the server. It does not say it should send the
data as soon as it has it available.

On the other hand, the property SendChuncked, does allow you to instruct
the WebRequest to send the data when it becomes available:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.sendchunked.aspx

On a completely different note... Instead of using Application.DoEvents,
it's better to use Begin/End-Write methods to send the data
asynchronously. The application would then not need to handle the events
explicitly. It is more difficult to implement though
 
C

Chris Morse

I think you've interpreted the actual meaning of the property
incorrectly. The documentation states that setting this property to
false, instructs the application to not keep a copy of the data around
after transferring it to the server. It does not say it should send the
data as soon as it has it available.

Everything I've read indicates that setting this property to False
will cause data to be written to the server immediately, without any
local buffering. MSDN mentions using this as a workaround to running
out of memory when sending large files (for example, 500MB files),
since there is no limit to the local memory used for buffering.
On the other hand, the property SendChuncked, does allow you to instruct
the WebRequest to send the data when it becomes available:http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.sen...

I'm aware of chunked uploads. I was hoping to figure out why this
method was not working when it (clearly, to me, anyhow) should and
did, a little while ago.

On another note, I just created a .NET 2.0 version of this test and I
get the exact same results. Frustrating, because I know this was
working in December when I last worked on it.

// CHRIS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top