向主机发送大量请求时,该操作在System.Net.HttpWebRequest.GetResponse()处超时 [英] The operation has timed out at System.Net.HttpWebRequest.GetResponse() while sending large number of requests to a host
问题描述
我正在向具有不同数据的特定Web服务发送大量同时请求.为了实现这一点,我创建了多个线程(数量大约为50).每分钟的请求总数可能会增加到10000. Windows服务形式的应用程序可以在几分钟内正常运行,然后遇到操作超时错误.
I am sending a large number of simultaneous requests to a particular web service with different data. To achieve this, I have created a number of threads(around 50 in number). Total number of requests per minute may increase up to 10000. The application in the form of a windows service runs fine for a few minutes and then a operation time out error is encountered.
我尝试了常见的可疑措施,例如增加 DefaultConnectionLimit ,关闭网络响应对象.由于请求在服务器上不需要花费很多时间,因此我还将请求 Timeout 和 ReadWriteTimeout 设置为5秒. 下面是由不同线程重复调用的代码段.
I have tried the usual suspects such as increasing DefaultConnectionLimit, closing the web response object. Since the requests do not take much time on server, I have also set the request Timeout and ReadWriteTimeout to 5 seconds. Below is the code snippet which is called repeatedly by different threads.
// Below line is executed at the start of application
ServicePointManager.DefaultConnectionLimit = 15000;
// Below code is executed at repeatedly by different threads
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Host = hostName;
request.Proxy = null;
request.UserAgent = "Windows Service";
byte[] bytes = new byte[0];
if (body != null)
{
bytes = System.Text.Encoding.ASCII.GetBytes(body);
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
}
request.Method = "POST";
request.Timeout = 5000;
request.ReadWriteTimeout = 5000;
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password));
request.CookieContainer = this.cookieContainer;
if (body != null)
{
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
}
HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseText = streamReader.ReadToEnd();
}
httpResponse.Close();
推荐答案
ServicePointManager.DefaultConnectionLimit
限制了发往给定服务器的Web请求的数量.默认设置通常为
ServicePointManager.DefaultConnectionLimit
limits the number of outgoing web requests to a given server. The default is generally 2 or 10.
如果要对该Web服务进行50次并行调用,则应将(应用程序启动时)ServicePointManager.DefaultConnectionLimit
设置为更大的数字(例如
If you are making 50 parallel calls to that web service, you should set ServicePointManager.DefaultConnectionLimit
(at app startup) to a larger number (e.g. 40-50).
此外,您没有在request
上调用Close
或Dispose
.您应该这样做,或者让 using
照顾好它为你.
Additionally, you are not calling Close
or Dispose
on request
. You should do this, or let using
take care of it for you.
这篇关于向主机发送大量请求时,该操作在System.Net.HttpWebRequest.GetResponse()处超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!