Task

Generally, this is a clone of the question on stackoverflow.com. (No one answers there)

It is necessary to download data in parts through one http request and show changes in progress after each download (physical data sending via the Internet). (It doesn't matter how I show progress, you can just output to the console).

Code

using System; using System.Text; using System.IO; using System.Net; ... public static void UploadData() { const string data = "simple string"; byte[] buffer = new ASCIIEncoding().GetBytes(data); // благодаря http://www.posttestserver.com все сразу работает HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://posttestserver.com/post.php"); req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 " + "(KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10"; req.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; req.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3"); req.Headers.Add("Accept-Language", "en-US,en;q=0.8"); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = buffer.Length; req.SendChunked = true; int bytesRead = buffer.Length; const int chunkSize = 3; Stream s = req.GetRequestStream(); for (int offset = 0; offset < bytesRead; offset += chunkSize) { int bytesLeft = bytesRead - offset; int bytesWrite = bytesLeft > chunkSize ? chunkSize : bytesLeft; s.Write(buffer, offset, bytesWrite); } s.Close(); // ВАЖНО: только здесь будут отправлены данные через сеть } 

Important

Whatever the SendChuncked parameters, AllowWriteStreamBuffering and ContentLength data are never sent over the network after each write to the request stream (everything looked through Fiddler).

Question

How to send data (physically) after each write to the request flow (call to the Write method)?

Limitations:

  • .Net 2.0;
  • use only HttpWebRequest (WebClient does not fit).
  • Doesn't Fiddler wait for the data to go completely to show the full request / response? Stream.Flush tried after Stream.Write ? - Zergatul
  • If you call Flush after Write, then Fiddler shows no activity. Fiddler shows activity only after calling Close (). - ordinary-developer
  • And yes, perhaps Fiddler is waiting for the data to go completely to show the full request / response. But if you look through the http debuger ( httpdebugger.com ), then the behavior is the same. Perhaps httpdebugger behaves like fiddler. I just need to somehow make sure that the data is actually sent after each call to Write. - ordinary-developer

1 answer 1

Everything works as you expect. I used Microsoft Network Monitor, this is a high-quality, low-level utility, also free (unlike httpdebugger). Stopped the program in debug (switched to .NET 2.0): enter image description here

Network Monitor shows every send by 3 bytes (I took a longer string):

enter image description here

For example, the text "ple" (sim ple string) is sent here: enter image description here