本文整理汇总了C#中HttpHeaders.Add方法的典型用法代码示例。如果您正苦于以下问题:C# HttpHeaders.Add方法的具体用法?C# HttpHeaders.Add怎么用?C# HttpHeaders.Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpHeaders
的用法示例。
在下文中一共展示了HttpHeaders.Add方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PopulateHeaders
public static void PopulateHeaders(this HttpPacket packet, HttpContentHeaders contentHeaders, HttpHeaders generalHeaders)
{
if (packet == null) throw new ArgumentNullException("packet");
string hdrKey;
foreach (var hdr in packet.Headers)
{
if (hdr.Key == null) continue;
hdrKey = hdr.Key.Trim().ToUpperInvariant();
if (hdrKey == "CONTENT-LENGTH") continue; //Content Length is automaitically calculated
if (Array.IndexOf<String>(contentOnlyHeaders, hdrKey) >= 0)
{
//TODO: Confirm if HttpResponseMessage/HttpRequestMessage will break headers into "," commas whereas in actuality header in Packet is an entire header
contentHeaders.Add(hdr.Key.Trim(), hdr.Value);
}
else
{
generalHeaders.Add(hdr.Key.Trim(), hdr.Value);
}
//TODO: Check if a string can be parsed properly into the typed header
//Test adding multiple headers of the same name will do. // Look up the Add overload that takes an ienumerable<string> to figure out its purpose.
}
}
示例2: Adding_nonstandard_header_name_is_ok
public void Adding_nonstandard_header_name_is_ok()
{
var headers = new HttpHeaders();
headers.Add("FOOBAR", "123");
headers.GetFirst<string>("FOOBAR").ShouldBe("123");
}
示例3: PopulateHeaders
/// <summary>
/// Populates contentheaders and generalheaders with headers from the <see cref="HttpPacket"/>>
/// </summary>
/// <param name="packet"></param>
/// <param name="contentHeaders"></param>
/// <param name="generalHeaders"></param>
public static void PopulateHeaders(this HttpPacket packet, HttpContentHeaders contentHeaders, HttpHeaders generalHeaders)
{
if (packet == null) throw new ArgumentNullException("packet");
bool dateHeaderProcessed = false;
string hdrKey;
foreach (var hdr in packet.Headers)
{
if (hdr.Key == null) continue;
hdrKey = hdr.Key.Trim().ToUpperInvariant();
if (hdrKey == "CONTENT-LENGTH")
{
continue; //Content Length is automatically calculated by System.Net.Http.ByteArrayContent
}
else if (hdrKey == "DATE")
{
if (dateHeaderProcessed) continue; //Already Processed
dateHeaderProcessed = true;
//Date Header in wrong format causes exception in System.Net.Http.HttpResponseMessage/HttpRequestMessage
//TODO: Confirm that this exception still occurs in the newer Nuget version of System.Net.Http
//Check if the date string is in RFC 1123 format
var val = (hdr.Value == null || !hdr.Value.Any()) ? null : hdr.Value.First().Trim();
if(val != null && Common.Shared.IsValidHttpDate(val))
{
generalHeaders.Add("Date", val);
}
continue;
}
if (Array.IndexOf<String>(contentOnlyHeaders, hdrKey) >= 0)
{
contentHeaders.Add(hdr.Key.Trim(), hdr.Value);
}
else
{
generalHeaders.Add(hdr.Key.Trim(), hdr.Value);
}
}
}
示例4: CopyTo
/// <summary>
/// Copies current header field to the provided <see cref="HttpHeaders"/> instance.
/// </summary>
/// <param name="headers">The headers.</param>
public void CopyTo(HttpHeaders headers)
{
headers.Add(_name.ToString(), _value.ToString().Trim(CurrentHeaderFieldStore._linearWhiteSpace));
Clear();
}
示例5: CopyTo
/// <summary>
/// Copies current header field to the provided <see cref="HttpHeaders"/> instance.
/// </summary>
/// <param name="headers">The headers.</param>
/// <param name="ignoreHeaderValidation">Set to false to validate headers</param>
public void CopyTo(HttpHeaders headers, bool ignoreHeaderValidation)
{
var name = _name.ToString();
var value = _value.ToString().Trim(CurrentHeaderFieldStore._linearWhiteSpace);
if (ignoreHeaderValidation)
{
headers.TryAddWithoutValidation(name, value);
}
else
{
headers.Add(name, value);
}
Clear();
}
示例6: AddMessageHeaders
private static void AddMessageHeaders(HttpHeaders headers)
{
headers.Add("N1", new string[] { "V1a", "V1b", "V1c", "V1d", "V1e" });
headers.Add("N2", "V2");
}
示例7: AddAuthCookie
private void AddAuthCookie(HttpHeaders requestHeaders, string cookieValue)
{
requestHeaders.Add("Cookie", String.Format("{0}={1}", IdentityCookieName, cookieValue));
}
示例8: AddCookiesToRequest
private void AddCookiesToRequest(HttpHeaders responseHeaders, HttpHeaders requestHeaders)
{
var cookiehHeaders = responseHeaders.GetValues("Set-Cookie");
foreach (var header in cookiehHeaders)
{
var cookieParts = header.Split(';');
var cookie = cookieParts[0];
var parts = cookie.Split('=');
requestHeaders.Add("Cookie", String.Format("{0}={1}", parts[0], parts[1]));
}
}
示例9: HttpHeadersAddTest
public void HttpHeadersAddTest()
{
var HttpHeaders = new HttpHeaders();
Assert.AreEqual("[]", HttpHeaders.AsEnumerable<HttpHeader>().ToJsonString());
HttpHeaders.Add("Content-Type", "text/html");
HttpHeaders.Set("Content-Type", "text/plain");
HttpHeaders.Set("cOntEnt-type", "text/plain");
Assert.AreEqual(1, HttpHeaders.Count());
Assert.AreEqual("cOntEnt-type: text/plain", String.Join("|", HttpHeaders.AsEnumerable()));
}