本文整理汇总了C#中HttpStatusCode.MayHaveBody方法的典型用法代码示例。如果您正苦于以下问题:C# HttpStatusCode.MayHaveBody方法的具体用法?C# HttpStatusCode.MayHaveBody怎么用?C# HttpStatusCode.MayHaveBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpStatusCode
的用法示例。
在下文中一共展示了HttpStatusCode.MayHaveBody方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: create
private static HttpResponseContent create(Func<Stream> getContentStream, string contentType, HttpStatusCode status, HttpResponseHeaders headers)
{
if (!status.MayHaveBody() && getContentStream != null)
throw new InvalidOperationException("A response with the {0} status cannot have a body.".Fmt(status));
if (!status.MayHaveBody() && (contentType != null || (headers != null && headers.ContentType != null)))
throw new InvalidOperationException("A response with the {0} status cannot have a Content-Type header.".Fmt(status));
headers = headers ?? new HttpResponseHeaders();
headers.ContentType = contentType ?? headers.ContentType;
return new HttpResponseContent(status, headers, getContentStream);
}
示例2: outputResponse
private bool outputResponse(HttpResponse response, HttpStatusCode status, HttpResponseHeaders headers, Stream contentStream, UseGzipOption useGzipOption, HttpRequest originalRequest)
{
// IMPORTANT: Do not access properties of ‘response’. Use the other parameters instead, which contain copies.
// The request handler can re-use HttpResponse objects between requests. Thus the properties of ‘response’ would
// be accessed simultaneously from multiple instances of this method running in separate threads.
// Additionally, HttpResponse is a MarshalByRefObject but HttpResponseHeaders is not, so ‘response’
// may be a transparent proxy, in which case ‘response.Headers’ would retrieve a serialized copy.
// ‘response’ is ONLY used to pass it to _server.ResponseExceptionHandler().
Socket.NoDelay = false;
try
{
bool gzipRequested = false;
if (originalRequest.Headers.AcceptEncoding != null)
foreach (HttpContentEncoding hce in originalRequest.Headers.AcceptEncoding)
gzipRequested = gzipRequested || (hce == HttpContentEncoding.Gzip);
bool contentLengthKnown = false;
long contentLength = 0;
// Find out if we know the content length
if (contentStream == null)
{
contentLength = 0;
contentLengthKnown = true;
}
else if (headers.ContentLength != null)
{
contentLength = headers.ContentLength.Value;
contentLengthKnown = true;
}
else
{
// See if we can deduce the content length from the stream
try
{
if (contentStream.CanSeek)
{
contentLength = contentStream.Length;
contentLengthKnown = true;
}
}
catch (NotSupportedException) { }
}
bool useKeepAlive =
originalRequest.HttpVersion == HttpProtocolVersion.Http11 &&
originalRequest.Headers.Connection.HasFlag(HttpConnection.KeepAlive) &&
!headers.Connection.HasFlag(HttpConnection.Close);
headers.Connection = useKeepAlive ? HttpConnection.KeepAlive : HttpConnection.Close;
headers.ContentLength = null;
// Special cases: status codes that may not have a body
if (!status.MayHaveBody())
{
if (contentStream != null)
throw new InvalidOperationException("A response with the {0} status cannot have a body (GetContentStream must be null or return null).".Fmt(status));
if (headers.ContentType != null)
throw new InvalidOperationException("A response with the {0} status cannot have a Content-Type header.".Fmt(status));
sendHeaders(status, headers);
return useKeepAlive;
}
// Special case: empty body
if (contentLengthKnown && contentLength == 0)
{
headers.ContentLength = 0;
sendHeaders(status, headers);
return useKeepAlive;
}
// If no Content-Type is given, use default
headers.ContentType = headers.ContentType ?? _server.Options.DefaultContentType;
// If we know the content length and the stream can seek, then we can support Ranges - but it's not worth it for less than 16 KB
if (originalRequest.HttpVersion == HttpProtocolVersion.Http11 && contentLengthKnown && contentLength > 16 * 1024 && status == HttpStatusCode._200_OK && contentStream.CanSeek)
{
headers.AcceptRanges = HttpAcceptRanges.Bytes;
// If the client requested a range, then serve it
if (status == HttpStatusCode._200_OK && originalRequest.Headers.Range != null)
{
// Construct a canonical set of satisfiable ranges
var ranges = new SortedList<long, long>();
foreach (var r in originalRequest.Headers.Range)
{
long rFrom = r.From == null || r.From.Value < 0 ? 0 : r.From.Value;
long rTo = r.To == null || r.To.Value >= contentLength ? contentLength - 1 : r.To.Value;
if (ranges.ContainsKey(rFrom))
ranges[rFrom] = Math.Max(ranges[rFrom], rTo);
else
ranges.Add(rFrom, rTo);
}
// If one of the ranges spans the complete file, don't bother with ranges
if (!ranges.ContainsKey(0) || ranges[0] < contentLength - 1)
{
// Make a copy of this so that we can modify Ranges while iterating over it
var rangeFroms = new List<long>(ranges.Keys);
//.........这里部分代码省略.........