如何加快GetResponse和GetRequest的速度?

时间:2013-10-21 19:54:09

标签: c# asp.net performance httpwebrequest

我在ASP.NET C#中有一个网站,它在一个地方运行得很慢。当我使用分析器检查它时,两个缓慢的点是GetResponse和GetRequest。我正在连接到第三方系统以获取日历上的可用性,因此我通过POST与他们进行通信。我们有一个ASP经典的对应物,看起来更快,但我没有简单的方法来描述它。

我发送一个字节编码的XML字符串,大约440字节,具体取决于一些事情,返回值低于2k。我在这里包含了大部分相关的例程。我尝试将默认代理设置为GetEmptyWebProxy,以防我在阅读时有所帮助。

处理请求/响应需要2到5秒。压力很大,因为老板正在计算河马的数量而且我无法获得经典版本的真实统计数据,所以这一点都是感知。

我的问题是这样的 - 我是否能尽可能快地进行,这是野兽的本质,还是我可以做些什么来加速这种通信?非常感谢任何帮助。

以下是一些代码:

    protected void UpdateInventory(DateTime _SelectedDate)
{
    // build request object
    Envelope _Request = new Envelope();
    _Request.Body = new Body();
    _Request.Body.QueryEvents = new QueryEvents();
    _Request.Header = new Header();

    //setup search criteria for API request
    _Request.Body.QueryEvents.EventRangeBeginDate = _SelectedDate.ToString(_DTFormat);
    _Request.Body.QueryEvents.EventRangeEndDate = _SelectedDate.AddDays(1).ToString(_DTFormat);
    _Request.Body.QueryEvents.EventTypeID = _EventTypeId;
    _Request.Header.SourceID = "BackOffice";
    _Request.Header.MessageID = 0;
    _Request.Header.MessageType = "QueryEvents";
    _Request.Header.TimeStamp = LocaleHelper.LocalNow.ToString(_DTFormat);
    _Request.Header.EchoData = "BO Calendar";

    // send API request
    HttpStatusCode _Status = GetGatewayInventory(_Request);
}

protected HttpStatusCode GetGatewayInventory(Envelope _InvRequest)
{
    // set up return value
    HttpStatusCode _RetVal = HttpStatusCode.Unused;

    // initialize global inventory object
    _Inventory = new Envelope();

    // serialize request object into XML
    XmlSerializer _Serializer = new XmlSerializer(_InvRequest.GetType());
    MemoryStream _Stream = new MemoryStream();
    _Serializer.Serialize(_Stream, _InvRequest);

    XmlDocument _Doc = new XmlDocument();
    _Stream.Position = 0;
    _Doc.Load(_Stream);

    // remove unneeded info.
    XmlNode _Node = _Doc.SelectSingleNode("/Envelope");
    XmlElement _ENode = (XmlElement)_Node;
    _ENode.RemoveAttribute("xmlns:xsi");
    _ENode.RemoveAttribute("xmlns:xsd");

    // clean up
    string _XmlStr = _Doc.InnerXml.Replace("\"1.0\"", "'1.0'");
    byte[] _Bytes = System.Text.Encoding.ASCII.GetBytes(_XmlStr);

    // send string to gateway
    // set web request
    string _GWHost = _GatewayHostLive;

    HttpWebRequest _req = (HttpWebRequest)WebRequest.Create(_GWHost);

    IWebProxy myProxy = GlobalProxySelection.GetEmptyWebProxy();
    GlobalProxySelection.Select = myProxy;
    _req.Proxy = myProxy;      

    _req.Method = "POST";
    _req.ContentLength = _Bytes.Length;
    _req.ContentType = "text/xml; encoding='utf-8'";

    Stream _RequestStream = _req.GetRequestStream();        
    _RequestStream.Write(_Bytes, 0, _Bytes.Length);
    _RequestStream.Close();

    using (HttpWebResponse _Resp = (HttpWebResponse)_req.GetResponse())
    {
        _RetVal = _Resp.StatusCode;

        if (_Resp.StatusCode == HttpStatusCode.OK)
        {
            Stream _respStream = _Resp.GetResponseStream();
            XmlTextReader _xmlreader = new XmlTextReader(_respStream);
            XmlDocument _RespXml = new XmlDocument();
            _RespXml.Load(_xmlreader);
            _xmlreader.Close();

            // deserialize back into object
            StringReader _sr = new StringReader(_RespXml.InnerXml);
            XmlSerializer _XmlSr = new XmlSerializer(_Inventory.GetType());
            XmlReader _Inreader = new XmlTextReader(_sr);
            _Inventory = (Envelope)_XmlSr.Deserialize(_Inreader);

            StripExtrasIfOnlyShowFirstAvailable(_Inventory, CountTotalTickets());

            grd_EventTimes.DataSource = _Inventory.Body.Events.Event;
            grd_EventTimes.DataBind();
            grd_EventTimes.Visible = true;
            if (_Inventory.Body.Events.Event.Count > 0)
                lbl_GatewayId.Text = "GN: " + _Inventory.Body.Events.Event[0].EventName + "  ID:" + _EventTypeId ; 
        }
        _Resp.Close();
    }
    // exit and return value
    return _RetVal;
}

1 个答案:

答案 0 :(得分:0)

您可以尝试通过ServicePointManager.UseNagleAlgorithm = false停用Nagle algorithm,请参阅MSDN

Nagle的(坏)效果可以为你的POST请求增加至少半秒的额外延迟。它与小的POST请求混合在一起,因为它们在等待(读取)响应之前会对底层TCP流进行两次写入。请参阅Nagle Wikipedia链接中的详细信息。