MSDN上說:When SendChunked is true, the request sends data to the Internet resource in segments. The Internet resource must support receiving chunked data.
傳統的send request的方式是在request的header頭添加ContentLength,然后把內容寫在request的body中寫入要發送的內容。而如果用了SendChunked的之后,就不用在header中規定ContentLength了。
往更深層次里說,其實SendChunked模式是在客戶端和服務器端建立了一個管道,字節流(其實是segment)通過這個管道發送到服務端。
最近有幸同時接觸到服務端和客戶端,對這個有了更具體的了解,尤其在代碼層面上:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
// http://www.cnblogs.com/AllanDragoon/p/3323370.html
request.KeepAlive = false;
request.ServicePoint.Expect100Continue = false;
request.AllowWriteStreamBuffering = false;
// Turn on support for GZipped response
request.AutomaticDecompression = DecompressionMethods.GZip;
// Request content type
request.ContentType = "application/octetstream";
// Request accept type
request.Accept = "application/xml";
// Set method
request.Method = method;
// 設置SendChunked為true而不必設ContentLength, 反之亦然
request.SendChunked = true;
using (var requestStream = request.GetRequestStream())
{
StreamUtil.CopyStream(stream, requestStream);
}
using (var response = request.GetResponse())
{
}
通過同時調試客戶端和服務端代碼,我發現,如果設SendChunked為true,那么當調用request.GetRequestStream的時候,客戶端會和服務端通信(我想可能是客戶端需呀和服務端建立管道連接);如果為false,則不會和服務端通信。
目前我還不知道如何在服務器端實現支持SendChunked。至少明白了當調用request.GetRequestStream的時候,客戶端會和服務端通信。
