当前位置: 首页>>代码示例>>C#>>正文


C# CookieContainer.GetCookies方法代码示例

本文整理汇总了C#中CookieContainer.GetCookies方法的典型用法代码示例。如果您正苦于以下问题:C# CookieContainer.GetCookies方法的具体用法?C# CookieContainer.GetCookies怎么用?C# CookieContainer.GetCookies使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CookieContainer的用法示例。


在下文中一共展示了CookieContainer.GetCookies方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: 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);
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:30,代码来源:CookieContainerTest.cs

示例2: 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);
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:33,代码来源:CookieContainerTest.cs

示例3: GetCookies_Invalid

 public static void GetCookies_Invalid()
 {
     CookieContainer cc = new CookieContainer();
     Assert.Throws<ArgumentNullException>(() => cc.GetCookies(null));
 }
开发者ID:noahfalk,项目名称:corefx,代码行数:5,代码来源:CookieContainerTest.cs

示例4: GetCookies_RemovesExpired_Cookies

        public static async void GetCookies_RemovesExpired_Cookies()
        {
            Cookie c1 = new Cookie("name1", "value", "", ".url1.com");
            Cookie c2 = new Cookie("name2", "value", "", ".url2.com");
            c1.Expires = DateTime.Now.AddSeconds(1); // The cookie will expire in 1 second

            CookieContainer cc = new CookieContainer();
            cc.Add(c1);
            cc.Add(c2);

            await Task.Delay(2000); // Sleep for 2 seconds to wait for the cookie to expire
            Assert.Equal(0, cc.GetCookies(new Uri("http://url1.com")).Count); // There should no longer be such a cookie
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:13,代码来源:CookieContainerTest.cs

示例5: 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]);
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:21,代码来源:CookieContainerTest.cs

示例6: VerifyGetCookies

        private static void VerifyGetCookies(CookieContainer cc1, Uri uri, Cookie[] expected)
        {
            CookieCollection cc2 = cc1.GetCookies(uri);
            Assert.Equal(expected.Length, cc2.Count);

            for (int i = 0; i < expected.Length; i++)
            {
                Cookie c1 = expected[i];
                Cookie c2 = cc2[i];
                Assert.Equal(c1.Name, c2.Name); // Primitive check for equality
                Assert.Equal(c1.Value, c2.Value);
            }
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:13,代码来源:CookieContainerTest.cs

示例7: 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);
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:27,代码来源:CookieContainerTest.cs

示例8: Add_ReachedMaxCountWithExpiredCookies_Added

        public static async void Add_ReachedMaxCountWithExpiredCookies_Added()
        {
            Cookie c1 = new Cookie("name1", "value", "", ".domain1.com");
            Cookie c2 = new Cookie("name2", "value", "", ".domain2.com");
            Cookie c3 = new Cookie("name3", "value", "", ".domain3.com");
            c1.Expires = DateTime.Now.AddSeconds(1); // The cookie will expire in 1 second

            CookieContainer cc = new CookieContainer(2);
            cc.Add(c1);
            cc.Add(c2);

            await Task.Delay(2000); // Sleep for 2 seconds to wait for the cookie to expire
            cc.Add(c3);
            Assert.Equal(0, cc.GetCookies(new Uri("http://domain1.com")).Count);
            Assert.Equal(c3, cc.GetCookies(new Uri("http://domain3.com"))[0]);
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:16,代码来源:CookieContainerTest.cs

示例9: 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);
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:17,代码来源:CookieContainerTest.cs

示例10: WriteLog

    void IHttpHandler.ProcessRequest(HttpContext Context)
    {
        string ServerURL = "";
        string ext = ".tunnel";
        string protocol = "http://";

        try
        {
            // Parsing incoming URL and extracting original server URL
            char[] URL_Separator = { '/' };
            string[] URL_List = Context.Request.Url.AbsoluteUri.Remove(0, protocol.Length).Split(URL_Separator);
            ServerURL = protocol + URL_List[2].Remove(URL_List[2].Length - ext.Length, ext.Length) + @"/";
            string URLPrefix = @"/" + URL_List[1] + @"/" + URL_List[2]; // Eg. "/handler/stg2web.tunnel";
            for ( int i = 3; i < URL_List.Length; i++ )
            {
                ServerURL += URL_List[i] + @"/";
            }
            ServerURL = ServerURL.Remove(ServerURL.Length -1, 1);
            WriteLog(ServerURL + " (" + Context.Request.Url.ToString() + ")");

            // Extracting POST data from incoming request
            Stream RequestStream = Context.Request.InputStream;
            byte[] PostData = new byte[Context.Request.InputStream.Length];
            RequestStream.Read(PostData, 0, (int) Context.Request.InputStream.Length);

            // Creating proxy web request
            HttpWebRequest ProxyRequest = (HttpWebRequest) WebRequest.Create(ServerURL);
            if (false)
            {
                ProxyRequest.Proxy = new WebProxy("proxy1:80", true);
            }
            ProxyRequest.Method = Context.Request.HttpMethod;
            ProxyRequest.UserAgent = Context.Request.UserAgent;
            CookieContainer ProxyCookieContainer = new CookieContainer();
            ProxyRequest.CookieContainer = new CookieContainer();
            ProxyRequest.CookieContainer.Add(ProxyCookieContainer.GetCookies(new Uri(ServerURL)));
            ProxyRequest.KeepAlive = true;

            // For POST, write the post data extracted from the incoming request
            if (ProxyRequest.Method == "POST")
            {
                ProxyRequest.ContentType = "application/x-www-form-urlencoded";
                ProxyRequest.ContentLength = PostData.Length;
                Stream ProxyRequestStream = ProxyRequest.GetRequestStream();
                ProxyRequestStream.Write(PostData, 0, PostData.Length);
                ProxyRequestStream.Close();
            }

            // Getting response from the proxy request
            HttpWebResponse ProxyResponse = (HttpWebResponse) ProxyRequest.GetResponse();

            if (ProxyRequest.HaveResponse)
            {
                // Handle cookies
                foreach(Cookie ReturnCookie in ProxyResponse.Cookies)
                {
                    bool CookieFound = false;
                    foreach(Cookie OldCookie in ProxyCookieContainer.GetCookies(new Uri(ServerURL)))
                    {
                        if (ReturnCookie.Name.Equals(OldCookie.Name))
                        {
                            OldCookie.Value = ReturnCookie.Value;
                            CookieFound = true;
                        }
                    }
                    if (!CookieFound)
                    {
                        ProxyCookieContainer.Add(ReturnCookie);
                    }
                }
            }

            Stream StreamResponse = ProxyResponse.GetResponseStream();
            int ResponseReadBufferSize = 256;
            byte[] ResponseReadBuffer = new byte[ResponseReadBufferSize];
            MemoryStream MemoryStreamResponse = new MemoryStream();

            int ResponseCount = StreamResponse.Read(ResponseReadBuffer, 0, ResponseReadBufferSize);
            while ( ResponseCount > 0 )
            {
                MemoryStreamResponse.Write(ResponseReadBuffer, 0, ResponseCount);
                ResponseCount = StreamResponse.Read(ResponseReadBuffer, 0, ResponseReadBufferSize);
            }

            byte[] ResponseData = MemoryStreamResponse.ToArray();
            string ResponseDataString = Encoding.ASCII.GetString(ResponseData);

            Context.Response.ContentType = ProxyResponse.ContentType;

            // While rendering HTML, parse and modify the URLs present
            if (ProxyResponse.ContentType.StartsWith("text/html"))
            {
                HTML.ParseHTML Parser = new HTML.ParseHTML();
                Parser.Source = ResponseDataString;
                while (!Parser.Eof())
                {
                    char ch = Parser.Parse();
                    if (ch == 0)
                    {
                        HTML.AttributeList Tag = Parser.GetTag();
//.........这里部分代码省略.........
开发者ID:michielvanschaik,项目名称:WebPerfOpt,代码行数:101,代码来源:ReverseProxyHandler.cs


注:本文中的CookieContainer.GetCookies方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。