我有一个控制台应用程序,使用20个左右的线程连接到远程Web服务器并发送相当小的大小的任意http请求,超过ssl 100%.远程Web服务器实际上是一个完整的负载平衡数据中心,其中包含高可用性系统,每秒可处理数十万个请求.这不是服务器或带宽问题.话虽如此,我没有运行它,也没有对它的配置有任何影响,所以即使我想,我也无法进行服务器端更改.
当使用fiddler运行应用程序时,应用程序执行速度惊人.当没有在小提琴手中运行时,它实际上要慢得多,以至于对于手头的任务毫无用处.它似乎也在某个时刻锁定在过程的早期,但这可能只是一个死锁问题,我还不确定.
无论如何,小提琴作为代理,无疑是以某种方式修改我的请求/连接,以确保良好的吞吐量,但我不知道它在做什么.我试图找出它,以便我可以强制我的.net应用程序模仿小提琴程序连接处理行为,而不必实际通过fiddler运行它
我已粘贴下面的连接代码.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; namespace Redacted { public class HiveCommunicator { public static IResponse SendRequest(IRequest request) { ServicePointManager.DefaultConnectionLimit = 60; ServicePointManager.Expect100Continue = false; string hostUrlString = string.Empty; if (request.SiteID <= 0) hostUrlString = string.Format("{0}://{1}{2}",request.UseSSL ? "https" : "http",DataCenters.GetCenter(request.DataCenter),request.Path); else hostUrlString = string.Format("{0}://{1}{2}",string.Format(request.Path,request.SiteID)); HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(hostUrlString); switch (request.ContentType) { default: case ContentTypes.XML: webRequest.ContentType = "application/xml"; break; case ContentTypes.JSON: webRequest.ContentType = "application/json"; break; case ContentTypes.BINARY: webRequest.ContentType = "application/octet-stream"; break; } if (request.RequiresAuthorizationToken) { AuthorizationToken tok = HiveAuthentication.GetToken(request.SiteID); if (tok == null) { return null; } webRequest.Headers.Add(HttpRequestHeader.Authorization,tok.Token); } bool UsesRequestBody = true; switch (request.HttpVerb) { case HttpVerbs.POST: webRequest.Method = "POST"; break; case HttpVerbs.DELETE: webRequest.Method = "DELETE"; UsesRequestBody = false; break; case HttpVerbs.PUT: webRequest.Method = "PUT"; break; default: case HttpVerbs.GET: webRequest.Method = "GET"; UsesRequestBody = false; break; } HttpWebResponse webResponse = null; Stream webRequestStream = null; byte[] webRequestBytes = null; if (UsesRequestBody) { webRequestBytes = request.RequestBytes; webRequest.ContentLength = webRequestBytes.Length; webRequestStream = webRequest.GetRequestStream(); for (int i = 0; i < webRequest.ContentLength; i++) { webRequestStream.WriteByte(webRequestBytes[i]); } } try { webResponse = (HttpWebResponse)webRequest.GetResponse(); } catch (WebException ex) { webResponse = (HttpWebResponse)ex.Response; } if (UsesRequestBody) { webRequestStream.Close(); webRequestStream.Dispose(); } IResponse respReturn = request.ParseResponse(webResponse); webResponse.Close(); return respReturn; } } }
解决方法
我感谢那些试图提供帮助的人们.不幸的是,这需要致电Microsoft专业支持.
即使我使用的是ServicePointManager.Expect100Continue = false;它发生在应用程序生命周期的后期.查看System.Net.Trace日志,我们看到expect-100 continue标头仍在使用(除了使用fiddler时).解决方案是将其放入app启动(在Main()中)
我还试图在关闭请求流之前读取响应流.
修好之后,一切都很快就加快了.该应用程序运行速度快得多,没有提琴手,这是我所期望的.
有几个人说要在HttpWebResponse上调用dispose.该类没有公共Dispose方法.我假设.Close()在内部调用.Dispose().