本文整理汇总了C#中System.Net.Http.HttpContent.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# HttpContent.GetType方法的具体用法?C# HttpContent.GetType怎么用?C# HttpContent.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpContent
的用法示例。
在下文中一共展示了HttpContent.GetType方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateWrapperAsync
public static async Task<PreventDisposeContentWrapper> CreateWrapperAsync(HttpContent wrappedContent)
{
if (wrappedContent == null)
{
return new PreventDisposeContentWrapper(new byte[0], null);
}
var bytes = await wrappedContent.ReadAsByteArrayAsync();
var wrapper = new PreventDisposeContentWrapper(bytes, wrappedContent.GetType());
foreach (var header in wrappedContent.Headers)
{
wrapper.Headers.Add(header.Key, header.Value);
}
return wrapper;
}
示例2: CreateErrorResponseAsync
internal static Task CreateErrorResponseAsync(HttpContextBase httpContextBase, HttpContent responseContent, HttpRequestMessage request, Exception exception)
{
Contract.Assert(httpContextBase != null);
Contract.Assert(responseContent != null);
Contract.Assert(exception != null);
Contract.Assert(request != null);
HttpResponseBase httpResponseBase = httpContextBase.Response;
HttpResponseMessage errorResponse = null;
HttpResponseException responseException = exception as HttpResponseException;
// Ensure all headers and content are cleared to eliminate any partial results.
ClearContentAndHeaders(httpResponseBase);
// If the exception we are handling is HttpResponseException,
// that becomes the error response.
if (responseException != null)
{
errorResponse = responseException.Response;
}
else
{
// The exception is not HttpResponseException.
// Create a 500 response with content containing an explanatory message and
// stack trace, subject to content negotiation and policy for error details.
try
{
MediaTypeHeaderValue mediaType = responseContent.Headers.ContentType;
string messageDetails = (mediaType != null)
? Error.Format(
SRResources.Serialize_Response_Failed_MediaType,
responseContent.GetType().Name,
mediaType)
: Error.Format(
SRResources.Serialize_Response_Failed,
responseContent.GetType().Name);
errorResponse = request.CreateErrorResponse(
HttpStatusCode.InternalServerError,
new InvalidOperationException(messageDetails, exception));
// CreateErrorResponse will choose 406 if it cannot find a formatter,
// but we want our default error response to be 500 always
errorResponse.StatusCode = HttpStatusCode.InternalServerError;
}
catch
{
// Failed creating an HttpResponseMessage for the error response.
// This can happen for missing config, missing conneg service, etc.
// Create an empty error response and return a non-faulted task.
CreateEmptyErrorResponse(httpResponseBase);
return TaskHelpers.Completed();
}
}
Contract.Assert(errorResponse != null);
CopyResponseStatusAndHeaders(httpContextBase, errorResponse);
// The error response may return a null content if content negotiation
// fails to find a formatter, or this may be an HttpResponseException without
// content. In either case, cleanup and return a completed task.
if (errorResponse.Content == null)
{
errorResponse.Dispose();
return TaskHelpers.Completed();
}
// Copy the headers from the newly generated HttpResponseMessage.
// We must ask the content for its content length because Content-Length
// is lazily computed and added to the headers.
var unused = errorResponse.Content.Headers.ContentLength;
CopyHeaders(errorResponse.Content.Headers, httpContextBase);
return CreateErrorResponseAsyncCore(errorResponse, httpResponseBase);
}