简体   繁体   中英

How to speed up slow GetResponse and GetRequest?

I have a web site in ASP.NET C# that is running pretty slow in one spot. When I used a profiler to examine it the two spots of slowness are GetResponse and GetRequest. I am connecting to a 3rd party system to get availability on a calendar so I'm communicating with them through a POST. We have a counterpart that is in ASP classic and it seems faster but I have no easy way to profile it.

I am sending an XML string byte-encoded that's around 440 bytes, depending on a few things and the return is under 2k. I'm including most of the relevant routines here. I tried setting the default Proxy to GetEmptyWebProxy in case that would help as I read it could.

It is taking 2 to 5 seconds to process the request/response. The pressure is on because the boss is counting hippopotamuses and I can't get real stats on the Classic version so it's all perception at this point.

My question is this - Am I going about as fast as I can and it's the nature of the beast or is there something I can do to speed up this communications? Any help greatly appreciated.

Here's some code:

    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;
}

You can try to disable the Nagle algorithm via ServicePointManager.UseNagleAlgorithm = false , see MSDN .

The (bad) effects of Nagle can add at least half a second of extra delay to your POST request. It mixes badly with small POST requests since those do two writes to the underlying TCP stream before waiting for (reading) the response. See details in the Nagle Wikipedia link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM