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


C# IHttpResponse.SendBody方法代码示例

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


在下文中一共展示了IHttpResponse.SendBody方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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>
        public override bool Process(IHttpRequest request, IHttpResponse 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:oblivious,项目名称:Oblivious,代码行数:56,代码来源:FileModule.cs

示例2: checkSendText

        private static void checkSendText(
			IHttpRequest request,
			IHttpResponse response,
			string text)
        {
            //if (!string.IsNullOrEmpty(text))
            {
                response.ContentType = @"text/html";

                if (!string.IsNullOrEmpty(request.Headers[@"if-Modified-Since"]))
                {
            #pragma warning disable 162
                    response.Status = HttpStatusCode.OK;
            #pragma warning restore 162
                }

                addNeverCache(response);

                if (request.Method != @"Headers" && response.Status != HttpStatusCode.NotModified)
                {
                    Trace.WriteLine(
                        string.Format(
                            @"[Web server] Sending text for URL '{0}': '{1}'.",
                            request.Uri.AbsolutePath,
                            text));

                    var buffer2 = getBytesWithBom(text);

                    response.ContentLength = buffer2.Length;
                    response.SendHeaders();

                    response.SendBody(buffer2, 0, buffer2.Length);
                }
                else
                {
                    response.ContentLength = 0;
                    response.SendHeaders();

                    Trace.WriteLine(@"[Web server] Not sending.");
                }
            }
        }
开发者ID:ultimategrandson,项目名称:ZetaHtmlEditControl,代码行数:42,代码来源:WebServer.cs

示例3: Send

 protected void Send(IHttpResponse response, Stream resourceStream, long length)
 {
   const int BUF_LEN = 8192;
   byte[] buffer = new byte[BUF_LEN];
   int bytesRead;
   while ((bytesRead = resourceStream.Read(buffer, 0, length > BUF_LEN ? BUF_LEN : (int) length)) > 0) // Don't use Math.Min since (int) length is negative for length > Int32.MaxValue
   {
     length -= bytesRead;
     response.SendBody(buffer, 0, bytesRead);
   }
 }
开发者ID:jgauffin,项目名称:MediaPortal-2,代码行数:11,代码来源:FanartAccessModule.cs

示例4: 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(IHttpRequest request, IHttpResponse 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 modified = DateTime.MinValue.ToUniversalTime();
			if (!string.IsNullOrEmpty(request.Headers["if-Modified-Since"]))
			{
                DateTime since = DateTime.Parse(request.Headers["if-Modified-Since"]).ToUniversalTime();
                if (modified > since)
                    response.Status = HttpStatusCode.NotModified;
			}

			response.AddHeader("Last-modified", modified.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:oblivious,项目名称:Oblivious,代码行数:44,代码来源:ResourceFileModule.cs

示例5: 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(IHttpRequest request, IHttpResponse 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 since = DateTime.Parse(request.Headers["if-Modified-Since"]).ToUniversalTime();
                        DateTime modified = File.GetLastWriteTime(path).ToUniversalTime();

                        // Truncate the subsecond portion of the time stamp (if present)
                        modified = new DateTime(modified.Year, modified.Month, modified.Day, modified.Hour,
                            modified.Minute, modified.Second, DateTimeKind.Utc);

                        if (modified > since)
                            response.Status = HttpStatusCode.NotModified;
                    }

                    // Fixed by Albert, Team MediaPortal: ToUniversalTime
                    if (_useLastModifiedHeader)
                        response.AddHeader("Last-modified", File.GetLastWriteTime(path).ToUniversalTime().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 process file.", err);
            }

            return true;
        }
开发者ID:BGCX261,项目名称:zma-svn-to-git,代码行数:65,代码来源:FileModule.cs

示例6: 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>
        public override bool Process(IHttpRequest request, IHttpResponse response, IHttpSession session)
        {
            if (!CanHandle(request.Uri))
                return false;

            try
            {
                string path = GetPath(request.Uri);
                string extension = GetFileExtension(path);

                // Directory names can be "C:\MyDirectory.ext", file names can be "C:\MyFileWithoutExt",
                // so the safest way to see if one of it exists, is by asking the windows file system.
                bool directory_exists = Directory.Exists(path);
                bool file_exists = File.Exists(path);

                if (!directory_exists && !file_exists)
                {
                    if (!path.EndsWith("favicon.ico"))
                        Aurora.Framework.MainConsole.Instance.Output("Failed to find " + path);
                    return false;
                    throw new NotFoundException("Failed to proccess request: " + path);
                }

                if (directory_exists)
                {
                    bool indexFound = false;

                    // Look for default index files
                    if (_defaultIndexFiles.Count > 0)
                    {
                        foreach (string index in _defaultIndexFiles)
                        {
                            // TODO: Does path always end with '/'?
                            if (File.Exists(path + index))
                            {
                                path = path + index;
                                extension = GetFileExtension(path);
                                indexFound = true;
                                break;
                            }
                        }
                    }

                    if (!indexFound)
                    {
                        // List directory
                        if (!_allowDirectoryListing)
                        {
                            throw new ForbiddenException("Directory listing not allowed");
                        }

                        string output = GetDirectoryListing(path, request.Uri);

                        response.ContentType = "text/html";
                        response.ContentLength = output.Length;
                        response.SendHeaders();

                        response.SendBody(System.Text.Encoding.Default.GetBytes(output));

                        return true;
                    }
                }

                if (extension == null && file_exists) extension = "default";

                if (!MimeTypes.ContainsKey(extension))
                {
                    if (_serveUnknownTypes)
                    {
                        extension = "default";
                    }
                    else throw new ForbiddenException("Forbidden file type: " + extension);
                }

                // Cgi file
                if (MimeTypes[extension].Equals("wwwserver/stdcgi"))
                {
                    if (!_cgiApplications.ContainsKey(extension))
                        throw new ForbiddenException("Unknown cgi file type: " + extension);

                    if (!File.Exists(_cgiApplications[extension]))
                        throw new InternalServerException("Cgi executable not found: " + _cgiApplications[extension]);

                    string output = CGI.Execute(_cgiApplications[extension], path, request);

                    response.ContentType = "text/html";

                    GetCgiHeaders(ref output, response);

                    response.ContentLength = output.Length;
                    response.SendHeaders();
                    response.SendBody(Encoding.Default.GetBytes(output));
                }
                else // other files
//.........这里部分代码省略.........
开发者ID:Gnu32,项目名称:Silverfin,代码行数:101,代码来源:AdvancedFileModule.cs

示例7: RequestHandler

        private void RequestHandler(IHttpClientContext context, IHttpRequest request, IHttpResponse response)
        {
            response.Status = HttpStatusCode.NotFound;

            if (!CanHandle(request.Uri))
                return;

            try
            {
                string path = GetPath(request.Uri);
                if (path == null)
                    return;
                string extension = GetFileExtension(path);
                if (extension == null)
                    return;

                if (MimeTypes.ContainsKey(extension))
                    response.ContentType = MimeTypes[extension];
                else
                    return;

                using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    response.Status = HttpStatusCode.OK;

                    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)
            {
                response.Status = HttpStatusCode.NotFound;
            }
        }
开发者ID:robincornelius,项目名称:omvviewer-light,代码行数:54,代码来源:FileHandler.cs

示例8: 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(IHttpRequest request, IHttpResponse 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;
      }

      // Albert, Team MediaPortal: No conversion from modifiedTime to UTC necessary because modifiedTime doesn't denote any
      // meaningful time here
      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:davinx,项目名称:MediaPortal-2,代码行数:46,代码来源:ResourceFileModule.cs


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