Example #1
0
        /// <summary>
        /// 获取Request的Size
        /// </summary>
        /// <param name="request">HttpRequest对象</param>
        /// <returns>请求的大小</returns>
        public static long CountRequestSize(this HttpRequest request)
        {
            MemoryStream ms = new MemoryStream();

            try
            {
                TextWriter writer = new StreamWriter(ms);
                writer.Write(request.HttpMethod + " " + request.Path);

                //如果请求类型为GET,需要计算Querystring的长度
                if (request.RequestType.Equals("get", StringComparison.CurrentCultureIgnoreCase))
                {
                    string queryStringText = request.GetQueryStringText();
                    if (!String.IsNullOrWhiteSpace(queryStringText))
                    {
                        writer.Write("?" + queryStringText);
                    }
                }
                //计算Header的长度
                string headerCombine = request.GetCombineHeaders(true);
                if (!String.IsNullOrWhiteSpace(headerCombine))
                {
                    writer.Write(headerCombine);
                }
                writer.Write("\r\n");
                writer.Flush();
                //结果为InputStream的长度+之前流的长度
                return(request.InputStream.Length + ms.Length);
            }
            finally
            {
                ms.Close();
            }
        }
Example #2
0
        long CountRequestSize(HttpRequest request)
        {
            MemoryStream ms = new MemoryStream();
            try
            {
                TextWriter writer = new StreamWriter(ms);
                writer.Write(request.HttpMethod + " " + request.Path);
                string queryStringText = request.GetQueryStringText();
                if (!String.IsNullOrWhiteSpace(queryStringText))
                {
                    writer.Write("?" + queryStringText);
                }
                string headerCombine = request.GetCombineHeaders(true);
                if (!String.IsNullOrWhiteSpace(headerCombine))
                {
                    writer.Write(headerCombine);
                }
                writer.Write("\r\n");
                writer.Flush();

                return request.InputStream.Length + ms.Length;
            }
            finally
            {
                ms.Close();
            }
        }