Stream.Flush()在HttpWebRequest.GetRequestStream中不起作用

时间:2017-09-27 08:17:24

标签: c# httpwebrequest

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Timeout = REQUEST_TIMEOUT;
request.Method = "POST";
request.ContentType = "application/form-data";

byte[] data = new byte[1024];
var readNumber = localFileStream.Read(data, 0, data.Length);
using (var stream = request.GetRequestStream())
{
    var len1 = readNumber / 2;
    stream.Write(data, 0, len1);
    stream.Flush();
    stream.Write(data, len1, readNumber - len1);
    stream.Flush();
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

当我执行stream.Flush()时,它不会被阻止。我认为这应该被阻止,直到所有数据都被发送。但似乎在request.GetResponse()执行时发送数据。我的理解有问题吗?或者我该怎么做才能达到这个效果。 谢谢你的启发。

1 个答案:

答案 0 :(得分:0)

设置 HttpWebRequest.SendChunked = true 和 {{1}}