
当使用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().
总结以上是内存溢出为你收集整理的如何使用HttpWebRequest创建一个C#应用程序就像fiddler一样全部内容,希望文章能够帮你解决如何使用HttpWebRequest创建一个C#应用程序就像fiddler一样所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)