HttpWebRequest中的SendChunked

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的时候,客户端会和服务端通信。

转载于:https://www.cnblogs.com/AllanDragoon/p/3327415.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值