[英]C# - Connection: keep-alive Header is Not Being Sent During HttpWebRequest
[英]HttpWebRequest keep alive - how is the connection reused by .net?
Hy,我在10个concurent线程中使用HttpWebRequest来下载图像列表。 我在hostName之后对图像进行了排序,因此每个线程都从相同的主机名获取图像。
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
myReq.ServicePoint.ConnectionLimit = 10;
myReq.Timeout = 2000;
myReq.KeepAlive = true;
HttpWebResponse myResp = (HttpWebResponse )myReq.GetResponse();
程序运行一段时间后,我不断获得超时异常。 我的想法是我得到了例外,因为主机服务器可能对来自同一用户的concurent连接有一些限制。
那么如何在.net中重用连接? 在我的程序中,每个线程都创建一个到主机名的新连接,或者由于KeepAlive属性而重用现有的连接?
似乎问题是一些使用http / 1.0的服务器。 HttpWebRequest 使用100 Continue行为,但服务器不支持它。
所以我将属性System.Net.ServicePointManager.Expect100Continue
更改为false
,然后一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.