本文整理汇总了C#中HttpHeaders.TryGetValues方法的典型用法代码示例。如果您正苦于以下问题:C# HttpHeaders.TryGetValues方法的具体用法?C# HttpHeaders.TryGetValues怎么用?C# HttpHeaders.TryGetValues使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpHeaders
的用法示例。
在下文中一共展示了HttpHeaders.TryGetValues方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetHeaderString
private static string GetHeaderString(HttpHeaders headers, string name)
{
IEnumerable<string> values;
if (headers.TryGetValues(name, out values))
{
return values.FirstOrDefault();
}
return null;
}
示例2: CheckAuthToken
/// <summary>
/// Checks for authorization header and then validates the token in the header
/// </summary>
/// <param name="theHeaders"></param>
/// <returns></returns>
private bool CheckAuthToken(HttpHeaders theHeaders)
{
var aValues = Enumerable.Empty<string>();
theHeaders.TryGetValues("Authorization", out aValues);
if (aValues == null)
return false;
var aValueList = aValues as IList<string> ?? aValues.ToList();
if (!aValueList.Any())
return false;
var aValue = aValueList.FirstOrDefault();
if (string.IsNullOrEmpty(aValue))
return false;
return _loginService.ValidateToken(aValue);
}
示例3: GetODataVersion
private static ODataVersion? GetODataVersion(HttpHeaders headers, params string[] headerNames)
{
foreach (string headerName in headerNames)
{
IEnumerable<string> values;
if (headers.TryGetValues(headerName, out values))
{
string value = values.FirstOrDefault();
if (value != null)
{
string trimmedValue = value.Trim(' ', ';');
try
{
return ODataUtils.StringToODataVersion(trimmedValue);
}
catch (ODataException)
{
// Parsing ODataVersion failed, try next header
}
}
}
}
return null;
}
示例4: ComputeCanonicalizedHeaders
private static string ComputeCanonicalizedHeaders(string httpMethod, HttpHeaders httpHeaders)
{
var headers = httpHeaders
.Where(x => x.Key.StartsWith("x-ms-"))
.OrderBy(x => x.Key);
var httpMethodToUpper = httpMethod.ToUpper();
var contentLength = httpMethodToUpper == "GET" ? string.Empty : "0";
IEnumerable<string> values;
if (httpHeaders.TryGetValues("Content-Length", out values))
contentLength = values.First();
var stringToHash = string.Format("{0}\n\n\n{1}\n\n\n\n\n\n\n\n\n", httpMethodToUpper, contentLength);
return headers.Aggregate(stringToHash, (current, header) => current + string.Format("{0}:{1}\n", header.Key.ToLower(), header.Value.First()));
}
示例5: TryGetHeader
private string TryGetHeader(HttpHeaders headers, string headerName)
{
IEnumerable<string> values;
if (headers.TryGetValues(headerName, out values))
{
return values.FirstOrDefault();
}
return String.Empty;
}
示例6: GetFirstHeaderValue
private string GetFirstHeaderValue(HttpHeaders headers, string key)
{
IEnumerable<string> headerValues = null;
if (headers.TryGetValues(key, out headerValues))
return headerValues.FirstOrDefault();
return string.Empty;
}
示例7: Contains
/// <summary>
/// Asserts the given <see cref="HttpHeaders"/> contain the given <paramref name="values"/>
/// for the given <paramref name="name"/>.
/// </summary>
/// <param name="headers">The <see cref="HttpHeaders"/> to examine. It cannot be <c>null</c>.</param>
/// <param name="name">The name of the header. It cannot be empty.</param>
/// <param name="values">The values that must all be present. It cannot be null.</param>
public void Contains(HttpHeaders headers, string name, params string[] values)
{
Assert.NotNull(headers);
Assert.False(String.IsNullOrWhiteSpace(name), "Test error: name cannot be empty.");
Assert.NotNull(values);
IEnumerable<string> headerValues = null;
bool foundIt = headers.TryGetValues(name, out headerValues);
Assert.True(foundIt);
foreach (string value in values)
{
Assert.Contains(value, headerValues);
}
}
示例8: ConvertToNameValueCollection
/// <summary>
/// Converts the specified headers to name value collection, where the value is the first header value encountered.
/// </summary>
/// <param name="headers">The headers to convert.</param>
/// <returns>A NameValueCollection of headers and a comma separated concatenation of their values.</returns>
private static NameValueCollection ConvertToNameValueCollection(HttpHeaders headers)
{
var collection = new NameValueCollection();
if (headers != null)
{
foreach (var header in headers)
{
var concatenatedValue = string.Empty;
IEnumerable<string> values;
if (headers.TryGetValues(header.Key, out values))
{
concatenatedValue = values.Aggregate(concatenatedValue, (current, value) => current + value + ",");
}
concatenatedValue = concatenatedValue.TrimEnd(',');
collection.Add(header.Key, concatenatedValue);
}
}
return collection;
}
示例9: Contains
/// <summary>
/// Asserts the given <see cref="HttpHeaders"/> contain the given <paramref name="values"/>
/// for the given <paramref name="name"/>.
/// </summary>
/// <param name="headers">The <see cref="HttpHeaders"/> to examine. It cannot be <c>null</c>.</param>
/// <param name="name">The name of the header. It cannot be empty.</param>
/// <param name="values">The values that must all be present. It cannot be null.</param>
public static void Contains(HttpHeaders headers, string name, params string[] values)
{
Assert.IsNotNull(headers, "Test error: headers cannot be null.");
Assert.IsFalse(string.IsNullOrWhiteSpace(name), "Test error: name cannot be empty.");
Assert.IsNotNull(values, "Test error: values cannot be null.");
IEnumerable<string> headerValues = null;
bool foundIt = headers.TryGetValues(name, out headerValues);
Assert.IsTrue(foundIt, "Headers did not contain a header named " + name);
CollectionAssert.IsSubsetOf(values.ToList(), headerValues.ToList(), "Headers did not contain any or all of the expected headers.");
}
示例10: GetHeaderValue
/// <summary>
/// Retrieve the string value associated with the header of the passed name.
/// </summary>
/// <param name="headers">Request headers to check.</param>
/// <param name="headerName">Name of the header.</param>
/// <exception cref="ArgumentNullException">Parameter headers is null.</exception>
/// <exception cref="InvalidOperationException">Duplicate headers were found.</exception>
/// <returns>String value associated with the header if found; null otherwise.</returns>
internal static string GetHeaderValue(HttpHeaders headers, string headerName)
{
if (headers == null)
{
throw new ArgumentNullException("headers");
}
string value = null;
IEnumerable<String> headerValues;
if (headers.TryGetValues(headerName, out headerValues))
{
value = headerValues.SingleOrDefault();
}
return value;
}
示例11: TryGetHeader
public IEnumerable<string> TryGetHeader(HttpHeaders headers)
{
Fx.Assert(headers != null, "The 'headers' parameter should never be null.");
IEnumerable<string> values = null;
if (!headers.TryGetValues(this.Name, out values))
{
values = null;
this.UpdateHeaderInfo(headers);
}
return values;
}
示例12: GetODataVersion
private static ODataVersion? GetODataVersion(HttpHeaders headers, string headerName)
{
ODataVersion? version = null;
IEnumerable<string> values;
if (headers.TryGetValues(headerName, out values))
{
foreach (string value in values)
{
string trimmedValue = value.Trim(' ', ';');
version = GetODataVersion(trimmedValue);
}
}
return version;
}