本文整理汇总了C#中System.Net.Http.Formatting.MediaTypeFormatter.SelectCharacterEncoding方法的典型用法代码示例。如果您正苦于以下问题:C# MediaTypeFormatter.SelectCharacterEncoding方法的具体用法?C# MediaTypeFormatter.SelectCharacterEncoding怎么用?C# MediaTypeFormatter.SelectCharacterEncoding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.Formatting.MediaTypeFormatter
的用法示例。
在下文中一共展示了MediaTypeFormatter.SelectCharacterEncoding方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SelectResponseCharacterEncoding
/// <summary>
/// Determine the best character encoding for writing the response. First we look
/// for accept-charset headers and if not found then we try to match
/// any charset encoding in the request (in case of PUT, POST, etc.)
/// If no encoding is found then we use the default for the formatter.
/// </summary>
/// <returns>The <see cref="Encoding"/> determined to be the best match.</returns>
protected virtual Encoding SelectResponseCharacterEncoding(HttpRequestMessage request, MediaTypeFormatter formatter)
{
if (request == null)
{
throw Error.ArgumentNull("request");
}
if (formatter == null)
{
throw Error.ArgumentNull("formatter");
}
// If there are any SupportedEncodings then we pick an encoding
if (formatter.SupportedEncodings.Count > 0)
{
// Sort Accept-Charset header values
IEnumerable<StringWithQualityHeaderValue> sortedAcceptCharsetValues = SortStringWithQualityHeaderValuesByQFactor(request.Headers.AcceptCharset);
// Check for match based on accept-charset headers
foreach (StringWithQualityHeaderValue acceptCharset in sortedAcceptCharsetValues)
{
foreach (Encoding encoding in formatter.SupportedEncodings)
{
if (encoding != null && acceptCharset.Quality != FormattingUtilities.NoMatch &&
(acceptCharset.Value.Equals(encoding.WebName, StringComparison.OrdinalIgnoreCase) ||
acceptCharset.Value.Equals("*", StringComparison.OrdinalIgnoreCase)))
{
return encoding;
}
}
}
// Check for match based on any request entity body
return formatter.SelectCharacterEncoding(request.Content != null ? request.Content.Headers : null);
}
return null;
}
示例2: SelectResponseCharacterEncoding
/// <summary>
/// Determine the best character encoding for writing the response. First we look
/// for accept-charset headers and if not found then we try to match
/// any charset encoding in the request (in case of PUT, POST, etc.)
/// If no encoding is found then we use the default for the formatter.
/// </summary>
/// <returns>The <see cref="Encoding"/> determined to be the best match.</returns>
protected virtual Encoding SelectResponseCharacterEncoding(
HttpRequestMessage request,
MediaTypeFormatter formatter)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (formatter == null)
{
throw new ArgumentNullException(nameof(formatter));
}
// If there are any SupportedEncodings then we pick an encoding
var supportedEncodings = formatter.SupportedEncodings.ToList();
if (supportedEncodings.Count > 0)
{
// Sort Accept-Charset header values
var sortedAcceptCharsetValues =
SortStringWithQualityHeaderValuesByQFactor(request.Headers.AcceptCharset);
// Check for match based on accept-charset headers
foreach (StringWithQualityHeaderValue acceptCharset in sortedAcceptCharsetValues)
{
for (var i = 0; i < supportedEncodings.Count; i++)
{
var encoding = supportedEncodings[i];
if (encoding != null && acceptCharset.Quality != FormattingUtilities.NoMatch &&
(acceptCharset.Value.Equals(encoding.WebName, StringComparison.OrdinalIgnoreCase) ||
acceptCharset.Value.Equals("*", StringComparison.Ordinal)))
{
return encoding;
}
}
}
// Check for match based on any request entity body
return formatter.SelectCharacterEncoding(request.Content != null ? request.Content.Headers : null);
}
return null;
}