Пример #1
0
        static async Task CheckImporters(ICookieImporter importer, CookieImportState expectedState, bool expectedIsAvailable, bool checkUserSession)
        {
            var cookies = new CookieContainer();
            var url     = new Uri("http://nicovideo.jp/");
            var res     = await importer.GetCookiesAsync(url);

            res.AddTo(cookies);

            LogWriter.WriteLine("{0}:\t{1} ({2})",
                                importer.IsAvailable ? "OK" : "Error", importer.SourceInfo.BrowserName, importer.SourceInfo.ProfileName);

            Assert.AreEqual(expectedState, res.Status);
            Assert.IsNotNull(importer.SourceInfo.BrowserName);
            Assert.IsNotNull(importer.SourceInfo.ProfileName);
            Assert.AreEqual(expectedIsAvailable, importer.IsAvailable);

            if (expectedIsAvailable)
            {
                Assert.IsNotNull(importer.SourceInfo.CookiePath,
                                 string.Format("{0}のSourceInfo.CookiePathにはプロフィール名が入りますが、取得できませんでした。", importer.SourceInfo.BrowserName));
                Assert.AreNotEqual(0, cookies.GetCookies(url).Count,
                                   string.Format("{0}からCookieを取得できませんでした。", importer.SourceInfo.BrowserName));

                if (checkUserSession)
                {
                    Assert.IsNotNull(cookies.GetCookies(new Uri("http://nicovideo.jp"))["user_session"]);
                }
            }
            else
            {
                Assert.AreEqual(0, cookies.GetCookies(url).Count,
                                string.Format("{0}からIsAvailableがfalse状態でCookieを取得する事は出来ませんが、取得されてしまっています。", importer.SourceInfo.BrowserName));
            }
        }
            static async Task <string> GetUserName(ICookieImporter cookieImporter)
            {
                try
                {
                    var url       = new Uri("http://www.nicovideo.jp/my/channel");
                    var container = new CookieContainer();
                    var client    = new HttpClient(new HttpClientHandler()
                    {
                        CookieContainer = container
                    });
                    var result = await cookieImporter.GetCookiesAsync(url);

                    if (result.AddTo(container) != CookieImportState.Success)
                    {
                        return(null);
                    }

                    var res = await client.GetStringAsync(url);

                    if (string.IsNullOrEmpty(res))
                    {
                        return(null);
                    }
                    var namem = Regex.Match(res, "nickname = \"([^<>]+)\";", RegexOptions.Singleline);
                    if (namem.Success)
                    {
                        return(namem.Groups[1].Value);
                    }
                    else
                    {
                        return(null);
                    }
                }
                catch (System.Net.Http.HttpRequestException) { return(null); }
            }
Пример #3
0
            static async Task <string> GetUserName(ICookieImporter cookieImporter)
            {
                try
                {
                    var url       = new Uri("https://www.nicovideo.jp/my/channel");
                    var container = new CookieContainer();
                    var client    = new HttpClient(new HttpClientHandler()
                    {
                        CookieContainer = container
                    });
                    var result = await cookieImporter.GetCookiesAsync(url);

                    if (result.Status != CookieImportState.Success)
                    {
                        return(null);
                    }
                    foreach (Cookie c in result.Cookies)
                    {
                        if (Regex.IsMatch(c.Name, "[^0-9a-zA-Z\\._\\-\\[\\]%#&=\":\\{\\} \\(\\)/\\?\\|]") ||
                            Regex.IsMatch(c.Value, "[^0-9a-zA-Z\\._\\-\\[\\]%#&=\":\\{\\} \\(\\)/\\?\\|]"))
                        {
                            util.debugWriteLine(c.Name + " " + c.Value);
                            continue;
                        }
                        try
                        {
                            container.Add(new Cookie(c.Name, c.Value, c.Path, c.Domain));
                        }
                        catch (Exception e)
                        {
                            util.debugWriteLine(e.Message + e.StackTrace + e.TargetSite + e.Source);
                        }
                    }

                    //                    if (result.AddTo(container) != CookieImportState.Success)
                    //                        return null;

                    var res = await client.GetStringAsync(url);

                    if (string.IsNullOrEmpty(res))
                    {
                        return(null);
                    }
                    var namem = Regex.Match(res, "nickname = \"([^<>]+)\";", RegexOptions.Singleline);
                    if (namem.Success)
                    {
                        return(namem.Groups[1].Value);
                    }
                    else
                    {
                        return(null);
                    }
                }
                catch (System.Net.Http.HttpRequestException) { return(null); }
            }
Пример #4
0
        async private Task <CookieContainer> getBrowserCookie(bool isSub)
        {
            var si = SourceInfoSerialize.load(isSub);

//			var importer = await SunokoLibrary.Application.CookieGetters.Default.GetInstanceAsync(si, false);
            ICookieImporter importer = await SunokoLibrary.Application.CookieGetters.Default.GetInstanceAsync(si, false).ConfigureAwait(false);

//			var importers = new SunokoLibrary.Application.CookieGetters(true, null);
//			var importera = (await SunokoLibrary.Application.CookieGetters.Browsers.IEProtected.GetCookiesAsync(TargetUrl));
//			foreach (var rr in importer.Cookies)
//				util.debugWriteLine(rr);
            //importer = await importers.GetInstanceAsync(si, true);
            if (importer == null)
            {
                return(null);
            }

            CookieImportResult result = await importer.GetCookiesAsync(TargetUrl).ConfigureAwait(false);

            if (result.Status != CookieImportState.Success)
            {
                return(null);
            }

            //if (result.Cookies["user_session"] == null) return null;
            //var cookie = result.Cookies["user_session"].Value;

            //util.debugWriteLine("usersession " + cookie);

            var cc = new CookieContainer();

            cc.PerDomainCapacity = 200;
            foreach (Cookie _c in result.Cookies)
            {
                try {
                    if (_c.Name == "age_auth" || _c.Name.IndexOf("user_session") > -1)
                    {
                        cc.Add(new Cookie(_c.Name, _c.Value, "/", ".nicovideo.jp"));
                    }
                } catch (Exception e) {
                    util.debugWriteLine("cookie add browser " + _c.ToString() + e.Message + e.Source + e.StackTrace + e.TargetSite);
                }
            }
//			result.AddTo(cc);

            var c       = cc.GetCookies(TargetUrl)["user_session"];
            var secureC = cc.GetCookies(TargetUrl)["user_session_secure"];

            //cc = setUserSession(cc, c, secureC);


            return(cc);
        }
            static async Task<string> GetUserName(ICookieImporter cookieImporter)
            {
                try
                {
                    var url = new Uri("http://www.nicovideo.jp/my/channel");
                    var container = new CookieContainer();
                    var client = new HttpClient(new HttpClientHandler() { CookieContainer = container });
                    var result = await cookieImporter.GetCookiesAsync(url);
                    if (result.AddTo(container) != CookieImportState.Success)
                        return null;

                    var res = await client.GetStringAsync(url);
                    if (string.IsNullOrEmpty(res))
                        return null;
                    var namem = Regex.Match(res, "nickname = \"([^<>]+)\";", RegexOptions.Singleline);
                    if (namem.Success)
                        return namem.Groups[1].Value;
                    else
                        return null;
                }
                catch (System.Net.Http.HttpRequestException) { return null; }
            }
        static async Task CheckImporters(ICookieImporter importer, CookieImportState expectedState, bool expectedIsAvailable, bool checkUserSession)
        {
            var cookies = new CookieContainer();
            var url = new Uri("http://nicovideo.jp/");
            var res = await importer.GetCookiesAsync(url);
            res.AddTo(cookies);

            LogWriter.WriteLine("{0}:\t{1} ({2})",
                importer.IsAvailable ? "OK" : "Error", importer.SourceInfo.BrowserName, importer.SourceInfo.ProfileName);

            Assert.AreEqual(expectedState, res.Status);
            Assert.IsNotNull(importer.SourceInfo.BrowserName);
            Assert.IsNotNull(importer.SourceInfo.ProfileName);
            Assert.AreEqual(expectedIsAvailable, importer.IsAvailable);

            if (expectedIsAvailable)
            {
                Assert.IsNotNull(importer.SourceInfo.CookiePath,
                    string.Format("{0}のSourceInfo.CookiePathにはプロフィール名が入りますが、取得できませんでした。", importer.SourceInfo.BrowserName));
                Assert.AreNotEqual(0, cookies.GetCookies(url).Count,
                    string.Format("{0}からCookieを取得できませんでした。", importer.SourceInfo.BrowserName));

                if (checkUserSession)
                    Assert.IsNotNull(cookies.GetCookies(new Uri("http://nicovideo.jp"))["user_session"]);
            }
            else
            {
                Assert.AreEqual(0, cookies.GetCookies(url).Count,
                    string.Format("{0}からIsAvailableがfalse状態でCookieを取得する事は出来ませんが、取得されてしまっています。", importer.SourceInfo.BrowserName));
            }
        }
Пример #7
0
            static async Task <string> GetUserName(ICookieImporter cookieImporter)
            {
                try
                {
                    var myPage = new Uri("https://www.nicovideo.jp/my/channel");

                    var container = new CookieContainer();
                    container.PerDomainCapacity = 200;
                    var client = new HttpClient(new HttpClientHandler()
                    {
                        CookieContainer = container, Proxy = util.httpProxy, UseProxy = true
                    });

                    var result = await cookieImporter.GetCookiesAsync(myPage);

                    if (result.Status != CookieImportState.Success)
                    {
                        return(null);
                    }
                    foreach (Cookie c in result.Cookies)
                    {
                        if (Regex.IsMatch(c.Name, "[^0-9a-zA-Z\\._\\-\\[\\]%#&=\":\\{\\} \\(\\)/\\?\\|]") ||
                            Regex.IsMatch(c.Value, "[^0-9a-zA-Z\\._\\-\\[\\]%#&=\":\\{\\} \\(\\)/\\?\\|]"))
                        {
                            util.debugWriteLine(c.Name + " " + c.Value);
                            continue;
                        }
                        if (c.Name != "user_session")
                        {
                            continue;
                        }
                        try {
                            container.Add(new Cookie(c.Name, c.Value, c.Path, c.Domain));
                        } catch (Exception e) {
                            util.debugWriteLine(e.Message + e.StackTrace + e.TargetSite + e.Source);
                        }
                    }

//                    if (result.AddTo(container) != CookieImportState.Success)
//                        return null;

                    var us = container.GetCookies(myPage)["user_session"];
                    if (us == null)
                    {
                        return(null);
                    }

                    var n = util.getMyName(container, us.Value);
                    return(n);

                    /*
                     * //if (n != null) return n;
                     * var res = await client.GetStringAsync(myPage);
                     * if (string.IsNullOrEmpty(res))
                     * return null;
                     * var namem = Regex.Match(res, "nickname = \"([^<>]+)\";", RegexOptions.Singleline);
                     * if (namem.Success)
                     * return namem.Groups[1].Value;
                     * else
                     * return null;
                     */
                }
                catch (System.Net.Http.HttpRequestException) { return(null); }
            }