当前位置: 首页>>代码示例>>C#>>正文


C# HttpResponse.SendHeaders方法代码示例

本文整理汇总了C#中HttpResponse.SendHeaders方法的典型用法代码示例。如果您正苦于以下问题:C# HttpResponse.SendHeaders方法的具体用法?C# HttpResponse.SendHeaders怎么用?C# HttpResponse.SendHeaders使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HttpResponse的用法示例。


在下文中一共展示了HttpResponse.SendHeaders方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Process

        /// <summary>
        /// Method that process the url
        /// </summary>
        /// <param name="request">Information sent by the browser about the request</param>
        /// <param name="response">Information that is being sent back to the client.</param>
        /// <param name="session">Session used to </param>
        /// <returns>true if this module handled the request.</returns>
        public override bool Process(HttpRequest request, HttpResponse response, IHttpSession session)
        {
            if(!CanHandle(request))
                return false;

            string path = request.Uri.AbsolutePath;
            string contentType;
            Stream resourceStream = GetResourceStream(path, out contentType);
            if(resourceStream == null)
                return false;

            response.ContentType = contentType;
            DateTime modifiedTime = DateTime.MinValue;
            if (!string.IsNullOrEmpty(request.Headers["if-modified-since"]))
            {
                DateTime lastRequest = DateTime.Parse(request.Headers["if-modified-since"]);
                if (lastRequest.CompareTo(modifiedTime) <= 0)
                    response.Status = HttpStatusCode.NotModified;
            }

            response.AddHeader("Last-modified", modifiedTime.ToString("r"));
            response.ContentLength = resourceStream.Length;
            response.SendHeaders();

            if (request.Method != "Headers" && response.Status != HttpStatusCode.NotModified)
            {
                byte[] buffer = new byte[8192];
                int bytesRead = resourceStream.Read(buffer, 0, 8192);
                while (bytesRead > 0)
                {
                    response.SendBody(buffer, 0, bytesRead);
                    bytesRead = resourceStream.Read(buffer, 0, 8192);
                }
            }

            return true;
        }
开发者ID:emperorstarfinder,项目名称:arribasim-libs,代码行数:44,代码来源:ResourceFileModule.cs

示例2: Process

        /// <summary>
        /// Method that process the Uri.
        /// </summary>
        /// <param name="request">Information sent by the browser about the request</param>
        /// <param name="response">Information that is being sent back to the client.</param>
        /// <param name="session">Session used to </param>
        /// <exception cref="InternalServerException">Failed to find file extension</exception>
        /// <exception cref="ForbiddenException">File type is forbidden.</exception>
        public override bool Process(HttpRequest request, HttpResponse response, IHttpSession session)
        {
            if (!CanHandle(request.Uri))
                return false;

            try
            {
                string path = GetPath(request.Uri);
                string extension = GetFileExtension(path);
                if (extension == null)
                    throw new InternalServerException("Failed to find file extension");

                if (MimeTypes.ContainsKey(extension))
                    response.ContentType = MimeTypes[extension];
                else
                    throw new ForbiddenException("Forbidden file type: " + extension);

                using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    if (!string.IsNullOrEmpty(request.Headers["if-modified-since"]))
                    {
                        DateTime lastRequest = DateTime.Parse(request.Headers["if-modified-since"]);
                        if (lastRequest.CompareTo(File.GetLastWriteTime(path)) <= 0)
                            response.Status = HttpStatusCode.NotModified;
                    }

                    if (_useLastModifiedHeader)
                        response.AddHeader("Last-modified", File.GetLastWriteTime(path).ToString("r"));
                    response.ContentLength = stream.Length;
                    response.SendHeaders();

                    if (request.Method != "Headers" && response.Status != HttpStatusCode.NotModified)
                    {
                        byte[] buffer = new byte[8192];
                        int bytesRead = stream.Read(buffer, 0, 8192);
                        while (bytesRead > 0)
                        {
                            response.SendBody(buffer, 0, bytesRead);
                            bytesRead = stream.Read(buffer, 0, 8192);
                        }
                    }
                }
            }
            catch (FileNotFoundException err)
            {
                throw new InternalServerException("Failed to proccess file.", err);
            }

            return true;
        }
开发者ID:emperorstarfinder,项目名称:arribasim-libs,代码行数:58,代码来源:FileModule.cs


注:本文中的HttpResponse.SendHeaders方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。