本文整理汇总了C#中CookieContainer.Add方法的典型用法代码示例。如果您正苦于以下问题:C# CookieContainer.Add方法的具体用法?C# CookieContainer.Add怎么用?C# CookieContainer.Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CookieContainer
的用法示例。
在下文中一共展示了CookieContainer.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetCookies_AddCookiesWithImplicitDomain_CookiesReturnedOnlyForExactDomainMatch
public void GetCookies_AddCookiesWithImplicitDomain_CookiesReturnedOnlyForExactDomainMatch()
{
const string SchemePrefix = "http://";
const string OriginalDomain = "contoso.com";
var container = new CookieContainer();
var cookie1 = new Cookie(CookieName1, CookieValue1);
var cookie2 = new Cookie(CookieName2, CookieValue2) { Version = 1 };
var uri = new Uri(SchemePrefix + OriginalDomain);
container.Add(uri, cookie1);
container.Add(uri, cookie2);
var cookies = container.GetCookies(uri);
Assert.Equal(2, cookies.Count);
Assert.Equal(OriginalDomain, cookies[CookieName1].Domain);
Assert.Equal(OriginalDomain, cookies[CookieName2].Domain);
uri = new Uri(SchemePrefix + "www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(0, cookies.Count);
uri = new Uri(SchemePrefix + "x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(0, cookies.Count);
uri = new Uri(SchemePrefix + "y.x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(0, cookies.Count);
uri = new Uri(SchemePrefix + "z.y.x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(0, cookies.Count);
}
示例2: CreateCount11Container
private static CookieContainer CreateCount11Container()
{
CookieContainer cc1 = new CookieContainer();
// Add(Cookie)
cc1.Add(c1);
cc1.Add(c2);
cc1.Add(c3);
cc1.Add(c4);
// Add(CookieCollection)
CookieCollection cc2 = new CookieCollection();
cc2.Add(c5);
cc2.Add(c6);
cc2.Add(c7);
cc1.Add(cc2);
// Add(Uri, Cookie)
cc1.Add(u4, c8);
cc1.Add(u4, c9);
// Add(Uri, CookieCollection)
cc2 = new CookieCollection();
cc2.Add(c10);
cc2.Add(c11);
cc1.Add(u5, cc2);
return cc1;
}
示例3: GetCookies_AddCookieVersion0WithExplicitDomain_CookieReturnedForDomainAndSubdomains
public void GetCookies_AddCookieVersion0WithExplicitDomain_CookieReturnedForDomainAndSubdomains()
{
const string SchemePrefix = "http://";
const string OriginalDomain = "contoso.com";
var container = new CookieContainer();
var cookie1 = new Cookie(CookieName1, CookieValue1) { Domain = OriginalDomain };
container.Add(new Uri(SchemePrefix + OriginalDomain), cookie1);
var uri = new Uri(SchemePrefix + OriginalDomain);
var cookies = container.GetCookies(uri);
Assert.Equal(1, cookies.Count);
Assert.Equal(OriginalDomain, cookies[CookieName1].Domain);
uri = new Uri(SchemePrefix + "www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(1, cookies.Count);
uri = new Uri(SchemePrefix + "x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(1, cookies.Count);
uri = new Uri(SchemePrefix + "y.x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(1, cookies.Count);
uri = new Uri(SchemePrefix + "z.y.x.www." + OriginalDomain);
cookies = container.GetCookies(uri);
Assert.Equal(1, cookies.Count);
}
示例4: AddCookieCollection_Success
public static void AddCookieCollection_Success()
{
CookieContainer cc = new CookieContainer();
CookieCollection cookieCollection = new CookieCollection();
cookieCollection.Add(new Cookie("name3", "value","/",".contoso.com"));
cc.Add(cookieCollection);
Assert.Equal(1, cc.Count);
}
示例5: CookiePortTest
public CookiePortTest()
{
_cc = new CookieContainer();
_cookie = new Cookie("name", "value1", "/path", "localhost");
// use both space and comma as delimiter
_cookie.Port = "\"80 110,1050, 1090 ,1100\"";
_cc.Add(new Uri("http://localhost/path"), _cookie);
}
示例6: Add_CookieVersion1AndRootDomainWithNoLeadingDot_ThrowsCookieException
public void Add_CookieVersion1AndRootDomainWithNoLeadingDot_ThrowsCookieException()
{
const string SchemePrefix = "http://";
const string OriginalDomain = "contoso.com";
var container = new CookieContainer();
var cookie = new Cookie(CookieName1, CookieValue1) { Version = 1, Domain = OriginalDomain };
var uri = new Uri(SchemePrefix + OriginalDomain);
Assert.Throws<CookieException>(() => container.Add(uri, cookie));
}
示例7: Add_ReachedMaxDomainCapacity_NotAdded
public static void Add_ReachedMaxDomainCapacity_NotAdded()
{
CookieContainer cc = new CookieContainer() { PerDomainCapacity = 1 };
cc.Add(c1); // Add a cookie to fill up the capacity
Assert.Equal(1, cc.Count);
cc.Add(c2); // Should not be added
Assert.Equal(1, cc.Count);
}
示例8: Add_ExpiredCookie_NotAdded
public static void Add_ExpiredCookie_NotAdded()
{
CookieContainer cc = new CookieContainer();
Cookie c1 = new Cookie("name1", "value", "", ".domain.com") { Expired = true };
Cookie c2 = new Cookie("name2", "value", "", ".domain.com");
cc.Add(c1);
cc.Add(c2);
// Ignores adding expired cookies
Assert.Equal(1, cc.Count);
Assert.Equal(c2, cc.GetCookies(new Uri("http://domain.com"))[0]);
// Manually expire cookie
c2.Expired = true;
cc.Add(c2);
Assert.Equal(0, cc.Count);
}
示例9: Capacity_ShrinkNoneExpired_RemovesAll
public static void Capacity_ShrinkNoneExpired_RemovesAll()
{
Cookie c1 = new Cookie("name1", "value", "", ".url1.com");
Cookie c2 = new Cookie("name2", "value", "", ".url2.com");
Cookie c3 = new Cookie("name3", "value", "", ".url3.com");
Cookie c4 = new Cookie("name4", "value", "", ".url4.com");
CookieContainer cc = new CookieContainer() { PerDomainCapacity = 1 };
cc.Add(c1);
cc.Add(c2);
cc.Add(c3);
cc.Add(c4);
// Shrink to a number less than the current count - since we have no cookies that have expired,
// we should just clear the whole thing except for a single cookie
cc.Capacity = 2;
Assert.Equal(2, cc.Capacity);
Assert.Equal(0, cc.Count);
}
示例10: GetAsync_SetCookieContainer_CookieSent
public async Task GetAsync_SetCookieContainer_CookieSent(string cookieName, string cookieValue)
{
var handler = new HttpClientHandler();
var cookieContainer = new CookieContainer();
cookieContainer.Add(Configuration.Http.RemoteEchoServer, new Cookie(cookieName, cookieValue));
handler.CookieContainer = cookieContainer;
using (var client = new HttpClient(handler))
{
using (HttpResponseMessage httpResponse = await client.GetAsync(Configuration.Http.RemoteEchoServer))
{
Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode);
string responseText = await httpResponse.Content.ReadAsStringAsync();
_output.WriteLine(responseText);
Assert.True(TestHelper.JsonMessageContainsKeyValue(responseText, cookieName, cookieValue));
}
}
}
示例11: Add_Cookie_Success
public static void Add_Cookie_Success()
{
Cookie c1 = new Cookie("name1", "value", "", "contoso.com");
Cookie c2 = new Cookie("name2", "", "", "contoso.com") { Secure = true };
Cookie c3 = new Cookie("name3", "value", "", "contoso.com") { Port = "\"80, 90, 100\"" };
Cookie c4 = new Cookie("name4", "value", "", ".contoso.com");
Cookie c5 = new Cookie("name5", "value", "", "127.0.0.1");
CookieContainer cc1 = new CookieContainer();
Assert.Equal(0, cc1.Count);
cc1.Add(c1);
cc1.Add(c2);
cc1.Add(c3);
cc1.Add(c4);
cc1.Add(c5);
Assert.Equal(5, cc1.Count);
}
示例12: GetCookies_DifferentPaths_GetsMatchedPathsIncludingEmptyPath
public static void GetCookies_DifferentPaths_GetsMatchedPathsIncludingEmptyPath()
{
// Internally, paths are stored alphabetically sorted - so expect a cookie collection sorted by the path of each cookie
// This test ensures that all cookies with paths (that the path specified by the uri starts with) are returned
Cookie c1 = new Cookie("name1", "value", "/aa", ".url.com");
Cookie c2 = new Cookie("name2", "value", "/a", ".url.com");
Cookie c3 = new Cookie("name3", "value", "/b", ".url.com"); // Should be ignored - no match with the URL's path
Cookie c4 = new Cookie("name4", "value", "/", ".url.com"); // Should NOT be ignored (has no path specified)
CookieContainer cc1 = new CookieContainer();
cc1.Add(c1);
cc1.Add(c2);
cc1.Add(c3);
cc1.Add(c4);
CookieCollection cc2 = cc1.GetCookies(new Uri("http://url.com/aaa"));
Assert.Equal(3, cc2.Count);
Assert.Equal(c1, cc2[0]);
Assert.Equal(c2, cc2[1]);
Assert.Equal(c4, cc2[2]);
}
示例13: AddCookieCollectionUri_Success
public static void AddCookieCollectionUri_Success()
{
Uri uri = new Uri("http://contoso.com");
String domain = "contoso.com";
CookieContainer cc1 = new CookieContainer();
CookieCollection cc2 = new CookieCollection();
cc2.Add(new Cookie("name1", "value") { Domain = domain });
cc2.Add(new Cookie("name2", "value") { Domain = domain });
cc1.Add(uri, cc2);
Assert.Equal(2, cc1.Count);
}
示例14: Add_VerificationFailedCookie_Throws
public static void Add_VerificationFailedCookie_Throws()
{
CookieContainer cc = new CookieContainer();
foreach (Cookie c in CookieTest.InvalidCookies())
{
Assert.Throws<CookieException>(() => cc.Add(c));
}
}
示例15: Add_SameCookieDifferentVairants_OverridesOlderVariant
public static void Add_SameCookieDifferentVairants_OverridesOlderVariant()
{
Uri uri = new Uri("http://domain.com");
Cookie c1 = new Cookie("name1", "value", "", ".domain.com"); // Variant = Plain
Cookie c2 = new Cookie("name1", "value", "", ".domain.com") { Port = "\"80\"" }; // Variant = RFC2965 (should override)
Cookie c3 = new Cookie("name1", "value", "", ".domain.com") { Port = "\"80, 90\"" }; // Variant = RFC2965 (should override)
Cookie c4 = new Cookie("name1", "value", "", ".domain.com") { Version = 1 }; // Variant = RFC2109 (should be rejected)
CookieContainer cc = new CookieContainer();
cc.Add(c1);
// Adding a newer variant should override an older one
cc.Add(c2);
Assert.Equal(c2.Port, cc.GetCookies(uri)[0].Port);
// Adding the same variant should override the existing one
cc.Add(c3);
Assert.Equal(c3.Port, cc.GetCookies(uri)[0].Port);
// Adding an older variant shold be rejected
cc.Add(c4);
Assert.Equal(c3.Port, cc.GetCookies(uri)[0].Port);
// Ensure that although we added 3 cookies, only 1 was actually added (the others were overriden or rejected)
Assert.Equal(1, cc.Count);
}