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