zoukankan      html  css  js  c++  java
  • Async处理http请求

    何谓Async?

    请参考 http://www.cnblogs.com/jesse2013/p/async-and-await.html。

    通过一下例子,看是怎样异步处理http请求的。

        public class ServerCmdHandle : HttpTaskAsyncHandler //继承异步处理方法
        {
    
        private async Task<string> DoAsync(HttpContext context, Object reqStr)
            {
                string recvInfo = System.Web.HttpUtility.UrlDecode(System.Text.Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(reqStr as string)));
                return await Task.Run(() =>
                {
                    return Global.bus.ProcessBusiness(context, recvInfo);//实现异步处理的方法
                });
            }
    
            public override async System.Threading.Tasks.Task ProcessRequestAsync(HttpContext context)
            {
                try
                {
                    string result = "";
                    if (context.Request.RequestType.CompareTo("GET") == 0)
                    {
                        result = await DoAsync(context, context.Request.QueryString["data"].ToString() as Object);
                    }
                    else if (context.Request.RequestType.CompareTo("POST") == 0)
                    {
                        System.IO.Stream resStream = context.Request.GetBufferlessInputStream();
                        System.IO.StreamReader reader = new System.IO.StreamReader(resStream, Encoding.Default);
                        string resInfo = reader.ReadToEnd();
                        if (resInfo.Length > 0)
                        {
                            result = await DoAsync(context, resInfo as Object);//异步等待处理结果
                        }
                    }
                    else
                    {
                        throw new Exception("user requset method error");
                    }
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(result);
                }
                catch (Exception ex)
                {
                    ServiceLogger.LOG_INFO("func=> ProcessRequestAsync:" + ex.Message, 0);
                }
    
            }
    
    
        }
  • 相关阅读:
    事件(五):事件委托
    事件(四):事件类型
    【[ZJOI2005]午餐】
    【[POI2006]OKR-Periods of Words】
    【2^k进制数】
    【[SHOI2015]脑洞治疗仪】
    【[NOI2016]区间】
    【[SHOI2014]概率充电器】
    【逛公园】
    【[CQOI2014]数三角形】
  • 原文地址:https://www.cnblogs.com/Yunshine-sina/p/5553659.html
Copyright © 2011-2022 走看看