向主机发送大量请求时,该操作在System.Net.HttpWebRequest.GetResponse()处超时

编程入门 行业动态 更新时间:2024-10-26 16:23:03
本文介绍了向主机发送大量请求时,该操作在System.Net.HttpWebRequest.GetResponse()处超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在向具有不同数据的特定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请求的数量.默认设置通常为 2或10 .

ServicePointManager.DefaultConnectionLimit limits the number of outgoing web requests to a given server. The default is generally 2 or 10.

如果要对该Web服务进行50次并行调用,则应将(应用程序启动时)ServicePointManager.DefaultConnectionLimit设置为更大的数字(例如 40-50 ).

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()处超时

本文发布于:2023-11-05 19:38:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561662.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主机   操作   System   GetResponse   HttpWebRequest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!