Пример #1
0
 /// <summary>
 /// Delete a cookie in the browser by passing in a copy of a cookie
 /// </summary>
 /// <param name="cookie">An object that represents a copy of the cookie that needs to be deleted</param>
 public void DeleteCookie(Cookie cookie)
 {
     if (cookie != null)
     {
         this.DeleteCookieNamed(cookie.Name);
     }
 }
 public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority)
 {
     // not use FormsCredentials unless you have implements a custom autentication.
     authCookie = null;
     user = password = authority = null;
     return false;
 }
 public bool GetFormsCredentials(out Cookie authCookie, out string user,
  out string password, out string authority)
 {
     authCookie = null;
     user = password = authority = null;
     return false;
 }
Пример #4
0
        public void CookieJar_Example_Weighted(int total, int choc1, int choc2, int expectedNumerator, int expectedDenominator)
        {
            // Given we have 2 jars
            // jar one contains 10 choc and 30 vanilla
            // jar two contains 20 choc and 20 vanilla
            // what is the probability of drawing a 
            // vanilla cookie from jar 1

            var sample1 = new WeightedSample<Cookie>("Jar1");
            var sample2 = new WeightedSample<Cookie>("Jar2");
            var hypos = new HypoSet<Cookie>("All");

            hypos.Add(sample1, sample2);

            var chocx = new Cookie() { F = 'C' };
            var vanix = new Cookie() { F = 'V' };

            var choc = It.Is(chocx);
            var vani = It.Is(vanix);

            sample1[chocx] = choc1;
            sample1[vanix] = total - choc1;
            sample2[chocx] = choc2;
            sample2[vanix] = total - choc2;

            sample1.ProbabilityOfEvent(choc);
            sample2.ProbabilityOfEvent(choc);

            var postProb = hypos.PosterierProbability(sample1, vani);

            Assert.That(postProb.Numerator, Is.EqualTo(expectedNumerator));
            Assert.That(postProb.Denominator, Is.EqualTo(expectedDenominator));
        }
Пример #5
0
        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);
        }
Пример #6
0
        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);
        }
Пример #7
0
 public static void Ctor_NameValuePath_Success()
 {
     Cookie c = new Cookie("hello", "goodbye", "foo");
     Assert.Equal("hello", c.Name);
     Assert.Equal("goodbye", c.Value);
     Assert.Equal("foo", c.Path);
 }
    private static void AddAuthenticationCookies(HttpContext context, System.Net.HttpWebRequest req)
    {
        // Copy user identity to new cookie
        if (context.User != null && context.User.Identity != null)
        {
            var cookiesString0 = req.Headers[HttpRequestHeader.Cookie];
            var cookies = CookieParser.Parse(cookiesString0).ToList();

            // Pass username
            var userCookie = new Cookie("") { Name = "username", Value = context.User.Identity.Name };
            cookies.Add(userCookie);// Always add because this

            // Pass user groups
            var windowsIdentity = context.User.Identity as System.Security.Principal.WindowsIdentity;
            var groupNames = new string[] { };
            if (windowsIdentity.Groups != null)
                groupNames = windowsIdentity.Groups.Select(g => g.Translate(typeof(System.Security.Principal.NTAccount)).Value).ToArray();
            var groupsCookie = new Cookie("") { Name = "usergroups", Value = string.Join(",", groupNames) };
            cookies.Add(groupsCookie);

            // Authentication ticket
            var ticket = TicketHandler.IssueTicket();
            var ticketCookie = new Cookie("") { Name = "winauthticket", Value = ticket };
            cookies.Add(ticketCookie);

            var cookiesString = CookieParser.ToString(cookies.ToArray());
            req.Headers[HttpRequestHeader.Cookie] = cookiesString;
        }
    }
Пример #9
0
 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);
 }
Пример #10
0
 public void ShouldAddCookie()
 {
     driver.Get(macbethPage);
     Cookie cookie = new Cookie("cookie", "monster");
     driver.Manage().AddCookie(cookie);
     Assert.That(driver.Manage().GetCookies().ContainsKey(cookie.Name),
         "Cookie was not added successfully");
 }
Пример #11
0
        public static void CommentUri_GetSet_Success()
        {
            Cookie c = new Cookie();
            Assert.Null(c.CommentUri);

            c.CommentUri = new Uri("http://hello.com");
            Assert.Equal(new Uri("http://hello.com"), c.CommentUri);
        }
Пример #12
0
 public HTTPWrapper(Cookie sharedCookie)
 {
     UseGZip = true;
     UseProxy = false;
     lastReq = new LastRequest();
     cookies = sharedCookie;
     retries = DEFAULT_RETRIES;
 }
Пример #13
0
 public HttpResponseState(HttpStatusCode statusCode, string statusDescription, Uri requestUrl, Header[] headers, Cookie[] cookies, string contentType)
 {
     StatusCode = statusCode;
     StatusDescription = statusDescription;
     RequestUrl = requestUrl;
     Headers = headers;
     Cookies = cookies;
     ContentType = contentType;
 }
Пример #14
0
 public void Save(Cookie cookie)
 {
     if (Response.Cookies.AllKeys.Contains(cookie.Name)) {
             // Class was not designed to handle multiple Saves()
             // Need it? _You_ implement it!
             throw new Exception("Cookie has already been saved this session.");
         }
         Response.Cookies.Add(cookie.HttpCookie());
 }
        void OnAuthRequestFinished(HTTPRequest req, HTTPResponse resp)
        {
            AuthRequest = null;
            string failReason = string.Empty;

            switch (req.State)
            {
                // The request finished without any problem.
                case HTTPRequestStates.Finished:
                    if (resp.IsSuccess)
                    {
                        Cookie = resp.Cookies != null ? resp.Cookies.Find(c => c.Name.Equals(".ASPXAUTH")) : null;

                        if (Cookie != null)
                        {
                            HTTPManager.Logger.Information("CookieAuthentication", "Auth. Cookie found!");

                            if (OnAuthenticationSucceded != null)
                                OnAuthenticationSucceded(this);

                            // return now, all other paths are authentication failures
                            return;
                        }
                        else
                            HTTPManager.Logger.Warning("CookieAuthentication", failReason = "Auth. Cookie NOT found!");
                    }
                    else
                        HTTPManager.Logger.Warning("CookieAuthentication", failReason = string.Format("Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
                                                        resp.StatusCode,
                                                        resp.Message,
                                                        resp.DataAsText));
                    break;

                // The request finished with an unexpected error. The request's Exception property may contain more info about the error.
                case HTTPRequestStates.Error:
                    HTTPManager.Logger.Warning("CookieAuthentication", failReason = "Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception"));
                    break;

                // The request aborted, initiated by the user.
                case HTTPRequestStates.Aborted:
                    HTTPManager.Logger.Warning("CookieAuthentication", failReason = "Request Aborted!");
                    break;

                // Ceonnecting to the server is timed out.
                case HTTPRequestStates.ConnectionTimedOut:
                    HTTPManager.Logger.Error("CookieAuthentication", failReason = "Connection Timed Out!");
                    break;

                // The request didn't finished in the given time.
                case HTTPRequestStates.TimedOut:
                    HTTPManager.Logger.Error("CookieAuthentication", failReason = "Processing the request Timed Out!");
                    break;
            }

            if (OnAuthenticationFailed != null)
                OnAuthenticationFailed(this, failReason);
        }
Пример #16
0
        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));
        }
Пример #17
0
 private static Dictionary<string, object> ProcessCookie(Cookie singleCookie)
 {
     Dictionary<string, object> rawCookie = new Dictionary<string, object>();
     rawCookie.Add("name", singleCookie.Name);
     rawCookie.Add("value", singleCookie.Value);
     rawCookie.Add("path", singleCookie.Path);
     rawCookie.Add("domain", singleCookie.Domain);
     rawCookie.Add("secure", singleCookie.Secure);
     return rawCookie;
 }
Пример #18
0
        public static void Comment_GetSet_Success()
        {
            Cookie c = new Cookie();
            Assert.Equal(string.Empty, c.Comment);

            c.Comment = "hello";
            Assert.Equal("hello", c.Comment);

            c.Comment = null;
            Assert.Equal(string.Empty, c.Comment);
        }
Пример #19
0
        public static void HttpOnly_GetSet_Success()
        {
            Cookie c = new Cookie();
            Assert.False(c.HttpOnly);

            c.HttpOnly = true;
            Assert.True(c.HttpOnly);

            c.HttpOnly = false;
            Assert.False(c.HttpOnly);
        }
Пример #20
0
        public void CookiePropertiesAreSetWhenInitializedThroughCtor()
        {
            // Arrange
            var cookie = new Cookie("name", "value", "www.foo.com", "/");

            // Assert
            Assert.Equal("name", cookie.Name);
            Assert.Equal("value", cookie.Value);
            Assert.Equal("www.foo.com", cookie.Domain);
            Assert.Equal("/", cookie.Path);
        }
Пример #21
0
        private static Cookie ConvertCookie(INancyCookie nancyCookie)
        {
            var cookie =
                new Cookie(nancyCookie.Name, nancyCookie.Value, nancyCookie.Path, nancyCookie.Domain);

            if (nancyCookie.Expires.HasValue)
            {
                cookie.Expires = nancyCookie.Expires.Value;
            }

            return cookie;
        }
Пример #22
0
 public void ShouldDeleteCookie()
 {
     driver.Get(macbethPage);
     Cookie cookieToDelete = new Cookie("chocolate", "rain");
     Cookie cookieToKeep = new Cookie("canIHaz", "Cheeseburguer");
     driver.Manage().AddCookie(cookieToDelete);
     driver.Manage().AddCookie(cookieToKeep);
     Dictionary<String, Cookie> cookies = driver.Manage().GetCookies();
     driver.Manage().DeleteCookie(cookieToDelete);
     cookies = driver.Manage().GetCookies();
     Assert.IsFalse(cookies.ContainsKey(cookieToDelete.Name),
         "Cookie was not deleted successfully");
     Assert.IsTrue(cookies.ContainsKey(cookieToKeep.Name),
         "Valid cookie was not returned");
 }
 public HttpResponseState(
     HttpStatusCode statusCode,
     string statusDescription,
     Uri requestUrl,
     Header[] headers,
     Cookie[] cookies,
     string contentType,
     string server,
     string contentEncoding,
     DateTime lastModified)
     : this(statusCode, statusDescription, requestUrl, headers, cookies, contentType)
 {
     Server = server;
     ContentEncoding = contentEncoding;
     LastModified = lastModified;
 }
Пример #24
0
    private static bool TryAddCookie(WebRequest webRequest, Cookie cookie)
    {
        HttpWebRequest httpRequest = webRequest as HttpWebRequest;
        if (httpRequest == null)
        {
            return false;
        }

        if (httpRequest.CookieContainer == null)
        {
            httpRequest.CookieContainer = new CookieContainer();
        }

        httpRequest.CookieContainer.Add(cookie);
        return true;
    }
Пример #25
0
        protected override WebResponse GetWebResponse(WebRequest request)
        {
            WebResponse response = base.GetWebResponse(request);
            String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];

            if (setCookieHeader != null)
            {
                //do something if needed to parse out the cookie.
                if (setCookieHeader != null)
                {
                    Cookie cookie = new Cookie(); //create cookie
                    this.CookieContainer.Add(cookie);
                }
            }
            return response;
        }
Пример #26
0
        public void reads_basic_cookie()
        {
            var value = Guid.NewGuid().ToString();

            TestApplication
                .Endpoints
                .GetByInput(new CookieInfo(), "GET", configure: request =>
                {
                    var cookie = new Cookie(CookieEndpoint.CookieName, value, "/", "localhost");

                    var cookies = new CookieContainer();
                    cookies.Add(cookie);

                    request.CookieContainer = cookies;
                }).ReadAsText().ShouldEqual(value);
        }
Пример #27
0
 public void ShouldDeleteCookieNamed()
 {
     driver.Get(macbethPage);
     Cookie cookieToDelete = new Cookie("answer", "42");
     Cookie cookieToKeep = new Cookie("question", "dunno");
     driver.Manage().AddCookie(cookieToDelete);
     driver.Manage().AddCookie(cookieToKeep);
     driver.Manage().DeleteCookieNamed(cookieToDelete.Name);
     Dictionary<String, Cookie> cookies = driver.Manage().GetCookies();
     Assert.IsTrue(cookies.ContainsKey(cookieToKeep.Name),
         "Valid cookie was not returned");
     Cookie deletedCookie;
     cookies.TryGetValue(cookieToDelete.Name, out deletedCookie);
     Assert.IsFalse(cookies.ContainsKey(cookieToDelete.Name),
         "Cookie was not deleted successfully: " + deletedCookie.ToString());
 }
        /// <summary>Constructor.</summary>
        /// <param name="cssSelector">The CSS selector used to retrieve the panel being resized.</param>
        /// <param name="cookieKey">The unique key to store the panel size within (null if saving not required).</param>
        protected PanelResizerBase(string cssSelector, string cookieKey)
        {
            // Setup initial conditions.
            this.cssSelector = cssSelector;
            panel = jQuery.Select(cssSelector);
            this.cookieKey = cookieKey;
            if (cookie == null)
            {
                cookie = new Cookie("PanelResizeStore");
                cookie.Expires = 5000;
            }

            // Wire up events.
            GlobalEvents.WindowResize += delegate { OnWindowResize(); };

            // Finish up.
            LoadSize();
        }
Пример #29
0
        public Cookie CreateCookie(ISystemTime clock)
        {
            var cookie = new Cookie(_settings.Name)
            {
                HttpOnly = _settings.HttpOnly,
                Secure = _settings.Secure,
                Domain = _settings.Domain
            };

            if(_settings.Path.IsNotEmpty())
            {
                cookie.Path = _settings.Path;
            }

            cookie.Expires = _settings.ExpirationFor(clock.UtcNow());

            return cookie;
        }
Пример #30
0
    public static Cookie Lock(object o) {
        // Create the cookie which will release the lock.
#if DEBUG_LOCK_TRACE
        LockTrace t;
        lock(c_Traces.SyncRoot) {
            t = GetTrace(o);
            if(t == null)
                c_Traces[new HashableWeakReference(o)] = t = new LockTrace(o);
        }
        Cookie cookie = new Cookie(o, t);
#else
        Cookie cookie = new Cookie(o);
#endif
#if DEBUG_LOCK_CONTENTION
        if(!Monitor.TryEnter(o)) {
            Thread owner = t.Owner;
            Debug.WriteLine(string.Format("Lock contention detected on object of type {0}:",
                o.GetType().ToString()), t.GetType().ToString());
            Debug.Indent();
            try {
                Debug.WriteLine(string.Format("Contending thread: {0}; Thread owning lock: {1}",
                    Thread.CurrentThread.Name, owner == null ? null : owner.Name));
            } finally {
                Debug.Unindent();
            }
            Monitor.Enter(o);
        }
#else
        // Acquire the lock.
        Monitor.Enter(o);
#endif
        try {
#if DEBUG_LOCK_TRACE
            t.TraceEnter();
#endif
            return cookie;
        } catch {
            // If there's an exception for any reason after the lock is acquired, make sure it's released.
            cookie.Dispose();
            throw;
        }
    }
Пример #31
0
        public static void InitVariable()
        {
            if (Directory.Exists(Properties.Settings.Default.BasePicPath))
            {
                BasePicPath = Properties.Settings.Default.BasePicPath;
            }
            else
            {
                BasePicPath = AppDomain.CurrentDomain.BaseDirectory + "Pic\\";
            }


            if (string.IsNullOrEmpty(Properties.Settings.Default.Bus))
            {
                Properties.Settings.Default.EnableBus = false;
            }
            if (string.IsNullOrEmpty(Properties.Settings.Default.BusEurope))
            {
                Properties.Settings.Default.EnableBusEu = false;
            }
            if (string.IsNullOrEmpty(Properties.Settings.Default.DB))
            {
                Properties.Settings.Default.EnableDB = false;
            }
            if (string.IsNullOrEmpty(Properties.Settings.Default.Library))
            {
                Properties.Settings.Default.EnableLibrary = false;
            }
            if (string.IsNullOrEmpty(Properties.Settings.Default.DMM))
            {
                Properties.Settings.Default.EnableDMM = false;
            }
            if (string.IsNullOrEmpty(Properties.Settings.Default.Jav321))
            {
                Properties.Settings.Default.Enable321 = false;
            }
            if (string.IsNullOrEmpty(Properties.Settings.Default.FC2))
            {
                Properties.Settings.Default.EnableFC2 = false;
            }
            Properties.Settings.Default.Save();

            //每页数目
            //Properties.Settings.Default.DisplayNumber = 100;
            //Properties.Settings.Default.FlowNum = 20;
            //Properties.Settings.Default.ActorDisplayNum = 30;
            Properties.Settings.Default.VedioType          = "0";
            Properties.Settings.Default.ShowViewMode       = "0";
            Properties.Settings.Default.OnlyShowPlay       = false;
            Properties.Settings.Default.OnlyShowSubSection = false;

            //添加演员分隔符
            if (!actorSplitDict.ContainsKey(0))
            {
                actorSplitDict.Add(0, new char[] { ' ', '/' });
            }
            if (!actorSplitDict.ContainsKey(1))
            {
                actorSplitDict.Add(1, new char[] { ' ', '/' });
            }
            if (!actorSplitDict.ContainsKey(2))
            {
                actorSplitDict.Add(2, new char[] { ' ', '/' });
            }
            if (!actorSplitDict.ContainsKey(3))
            {
                actorSplitDict.Add(3, new char[] { '/' }); //欧美
            }
            FormatUrl();                                   //格式化网址


            RootUrl = new rootUrl
            {
                Bus     = Properties.Settings.Default.Bus,
                BusEu   = Properties.Settings.Default.BusEurope,
                Library = Properties.Settings.Default.Library,
                FC2     = Properties.Settings.Default.FC2,
                Jav321  = Properties.Settings.Default.Jav321,
                DMM     = Properties.Settings.Default.DMM,
                DB      = Properties.Settings.Default.DB
            };

            EnableUrl = new enableUrl
            {
                Bus     = Properties.Settings.Default.EnableBus,
                BusEu   = Properties.Settings.Default.EnableBusEu,
                Library = Properties.Settings.Default.EnableLibrary,
                FC2     = Properties.Settings.Default.EnableFC2,
                Jav321  = Properties.Settings.Default.Enable321,
                DMM     = Properties.Settings.Default.EnableDMM,
                DB      = Properties.Settings.Default.EnableDB
            };

            AllCookies = new Cookie
            {
                Bus     = "",
                BusEu   = "",
                Library = "",
                FC2     = "",
                Jav321  = "",
                DMM     = Properties.Settings.Default.DMMCookie,
                DB      = Properties.Settings.Default.DBCookie
            };

            GenreEurope[0] = Resource_String.GenreEurope.Split('|')[0];
            GenreEurope[1] = Resource_String.GenreEurope.Split('|')[1];
            GenreEurope[2] = Resource_String.GenreEurope.Split('|')[2];
            GenreEurope[3] = Resource_String.GenreEurope.Split('|')[3];
            GenreEurope[4] = Resource_String.GenreEurope.Split('|')[4];
            GenreEurope[5] = Resource_String.GenreEurope.Split('|')[5];
            GenreEurope[6] = Resource_String.GenreEurope.Split('|')[6];
            GenreEurope[7] = Resource_String.GenreEurope.Split('|')[7];

            GenreCensored[0] = Resource_String.GenreCensored.Split('|')[0];
            GenreCensored[1] = Resource_String.GenreCensored.Split('|')[1];
            GenreCensored[2] = Resource_String.GenreCensored.Split('|')[2];
            GenreCensored[3] = Resource_String.GenreCensored.Split('|')[3];
            GenreCensored[4] = Resource_String.GenreCensored.Split('|')[4];
            GenreCensored[5] = Resource_String.GenreCensored.Split('|')[5];
            GenreCensored[6] = Resource_String.GenreCensored.Split('|')[6];

            GenreUncensored[0] = Resource_String.GenreUncensored.Split('|')[0];
            GenreUncensored[1] = Resource_String.GenreUncensored.Split('|')[1];
            GenreUncensored[2] = Resource_String.GenreUncensored.Split('|')[2];
            GenreUncensored[3] = Resource_String.GenreUncensored.Split('|')[3];
            GenreUncensored[4] = Resource_String.GenreUncensored.Split('|')[4];
            GenreUncensored[5] = Resource_String.GenreUncensored.Split('|')[5];
            GenreUncensored[6] = Resource_String.GenreUncensored.Split('|')[6];
            GenreUncensored[7] = Resource_String.GenreUncensored.Split('|')[7];
        }
Пример #32
0
 public bool CanSetCookie(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, Cookie cookie)
 {
     return(false);
 }
 /// <summary>
 /// 开始下载
 /// </summary>
 public void Start()
 {
     try
     {
         Downloading = true;
         Stoped      = false;
         HttpWebRequest Request = WebRequest.Create(Url) as HttpWebRequest;
         Request.Referer = "http://pan.baidu.com/disk/home";
         //第一次下载设置Cookies
         if (Cookies != null)
         {
             Cookie ck = new Cookie("BDUSS", Cookies.BDUSS);
             ck.Domain = ".baidu.com";
             Request.CookieContainer = new CookieContainer();
             Request.CookieContainer.Add(ck);
             ck        = new Cookie("pcsett", Cookies.PCSETT);
             ck.Domain = ".baidu.com";
             Request.CookieContainer.Add(ck);
         }
         //改为获取Response之前读入数据文件,这样就能读取到Cookies了
         if (File.Exists(DownloadPath + ".dcj"))
         {
             Info = JsonConvert.DeserializeObject <DownloadInfo>(File.ReadAllText(DownloadPath + ".dcj"));
             if (Info.Cookies != null)
             {
                 Cookie ck = new Cookie("BDUSS", Info.Cookies.BDUSS);
                 ck.Domain = ".baidu.com";
                 Request.CookieContainer = new CookieContainer();
                 Request.CookieContainer.Add(ck);
                 ck        = new Cookie("pcsett", Info.Cookies.PCSETT);
                 ck.Domain = ".baidu.com";
                 Request.CookieContainer.Add(ck);
             }
         }
         HttpWebResponse Response = Request.GetResponse() as HttpWebResponse;
         if (!File.Exists(DownloadPath + ".dcj"))
         {
             Info = new DownloadInfo
             {
                 ContentLength = Response.ContentLength,
                 BlockLength   = Response.ContentLength / ThreadNum,
                 DownloadUrl   = Url,
                 Cookies       = Cookies
             };
             Info.init(DownloadPath + ".dcj");
         }
         if (Info.Completed)
         {
             Downloading = false;
             Completed   = true;
             Percentage  = 100F;
             return;
         }
         if (!File.Exists(DownloadPath))
         {
             LogTool.WriteLogDebug(typeof(HttpDownload), "正在创建文件: " + DownloadPath);
             FileStream Stream = new FileStream(DownloadPath, FileMode.CreateNew);
             Stream.SetLength(Response.ContentLength);
             Stream.Close();
         }
         Threads = new DownloadThread[Info.DownloadBlockList.Count];
         for (int i = 0; i < Info.DownloadBlockList.Count; i++)
         {
             DownloadBlock Block = JsonConvert.DeserializeObject <DownloadBlock>(Info.DownloadBlockList[i].ToString());
             Threads[i] = new DownloadThread
             {
                 ID          = i,
                 DownloadUrl = Url,
                 Path        = DownloadPath,
                 Block       = Block,
                 Info        = Info
             };
             Threads[i].ThreadCompletedEvent += HttpDownload_ThreadCompletedEvent;
         }
         new Thread(a).Start();
     }
     catch (Exception ex)
     {
         Downloading = false;
         Stoped      = true;
         LogTool.WriteLogError(typeof(HttpDownload), "创建下载任务出现错误", ex);
     }
 }
Пример #34
0
 /// <summary>
 /// 设置Cookie
 /// </summary>
 /// <param name="cookie">cookie</param>
 public TRequest Cookie(Cookie cookie)
 {
     _cookieContainer.Add(new Uri(_url), cookie);
     return(This());
 }
Пример #35
0
        public static Cookie ParseCookieNetscapeFormatLine(string cookieLine)
        {
            Cookie resCookie = new Cookie();

            if (cookieLine.Length == 0)
            {
                return(null);
            }

            if (cookieLine[0] == '#')
            {
                //  IE introduced HTTP-only cookies to prevent XSS attacks. Cookies
                //  marked with httpOnly after the domain name are not accessible
                //  from javascripts, but since curl does not operate at javascript
                //  level, we include them anyway. In Firefox's cookie files, these
                //  lines are preceeded with #HttpOnly_ and then everything is
                //  as usual, so we skip 10 characters of the line..

                if (cookieLine.StartsWith("#HttpOnly_"))
                {
                    cookieLine         = cookieLine.Substring(10);
                    resCookie.HttpOnly = true;//native curl just skip this and doesn't remember it
                }
                else
                {
                    //It's a comment
                    return(null);
                }
            }

            //Trim end of line characters
            cookieLine.TrimEnd('\n', '\r');

            String[] parts = cookieLine.Split('\t');

            // quick check to eliminate normal HTTP-headers from this ??

            int field = 0;

            foreach (string part in parts)
            {
                switch ((NetscapeCookieToken)field)
                {
                case NetscapeCookieToken.Domain:
                    //
                    //if (part[0] == '.')
                    //    resCookie.Domain = part.Substring(1);
                    //else
                    //    resCookie.Domain = part;

                    resCookie.Domain = part;
                    break;

                case NetscapeCookieToken.TailMatch:

                    // This field got its explanation on the 23rd of May 2001 by
                    //Andrés García:

                    //flag: A TRUE/FALSE value indicating if all machines within a given
                    //domain can access the variable. This value is set automatically by
                    //the browser, depending on the value you set for the domain.

                    //As far as I can see, it is set to true when the cookie says
                    //.domain.com and to false when the domain is complete www.domain.com

                    //skiping this for now
                    break;

                case NetscapeCookieToken.Path:

                    //Path part

                    /* It turns out, that sometimes the file format allows the path
                     * field to remain not filled in, we try to detect this and work
                     * around it! Andrés García made us aware of this... */
                    if (!part.Equals("TRUE", StringComparison.OrdinalIgnoreCase) &&
                        !part.Equals("FALSE", StringComparison.OrdinalIgnoreCase))
                    {
                        /* only if the path doesn't look like a boolean option! */
                        resCookie.Path = part;
                    }
                    else
                    {
                        /* this doesn't look like a path, make one up! */
                        resCookie.Path = "/";

                        /* add a field and fall down to secure */
                        /* FALLTHROUGH */
                        ++field;
                        goto case NetscapeCookieToken.Secure;
                    }

                    break;

                case NetscapeCookieToken.Secure:

                    if (part.Equals("TRUE", StringComparison.OrdinalIgnoreCase))
                    {
                        resCookie.Secure = true;
                    }

                    break;

                case NetscapeCookieToken.Expires:

                    long expires;

                    if (long.TryParse(part, out expires))
                    {
                        resCookie.Expires = new DateTime(1970, 1, 1).AddSeconds(expires);
                    }
                    else
                    {
                        //Not valid cookie
                        return(null);
                    }
                    break;

                case NetscapeCookieToken.Name:
                    resCookie.Name = part;
                    break;

                case NetscapeCookieToken.Value:
                    resCookie.Value = part;
                    break;
                }

                ++field;
            }

            return(resCookie);
        }
Пример #36
0
 private void SetCookie(Cookie cookie, string server)
 {
     _cookies.DeleteCookies($"https://www.{server}.darkorbit.com/", "dosid", null);
     _cookies.SetCookie($"https://www.{server}.darkorbit.com", cookie, null);
 }
Пример #37
0
            private List <Cookie> GetCookieCollectionInternal(string query)
            {
                System.Data.DataTable dt = null;
                using (var tempFile = new TempFileProvider())
                {
                    try
                    {
                        System.IO.File.Copy(Path, tempFile.Path, true);
                    }
                    catch (System.IO.FileNotFoundException ex)
                    {
                        var str = "";
                        try
                        {
                            var defaultDir  = System.IO.Path.GetDirectoryName(Path);
                            var userDataDir = System.IO.Directory.GetParent(defaultDir);
                            if (System.IO.Directory.Exists(userDataDir.FullName))
                            {
                                str = GetFiles(userDataDir.FullName);
                            }
                        }
                        catch (Exception)
                        {
                            str = "(error)";
                        }
                        throw new ChromeCookiesFileNotFoundException(str, ex);
                    }
                    using (var conn = SQLiteHelper.CreateConnection(tempFile.Path))
                    {
                        dt = SQLiteHelper.ExecuteReader(conn, query);
                    }
                }
                var list = new List <Cookie>();

                if (dt != null)
                {
                    var cc = new CookieContainer();
                    foreach (System.Data.DataRow row in dt.Rows)
                    {
                        var value = row["value"].ToString();
                        if (string.IsNullOrEmpty(value))//暗号化してるっぽいから復号化してみる。
                        {
                            var encrypted_value = (byte[])row["encrypted_value"];
                            value = UnProtect(encrypted_value);
                        }
                        //ここでURLエンコードをやると、ふわっちでAPIが取得できなかった。ここではやるべきではないのかも。
                        //if (value != null)
                        //    value = Uri.EscapeDataString(value);
                        var name        = row["name"].ToString();
                        var host_key    = row["host_key"].ToString();
                        var path        = row["path"].ToString();
                        var expires_utc = long.Parse(row["expires_utc"].ToString());
                        var cookie      = new Cookie(name, value, path, host_key)
                        {
                            //TODO:expires_utcの変換はこれで大丈夫だろうか。正しい値を取得できているか確認していない。
                            Expires = Tools.FromUnixTime(expires_utc / 1000000L - 11644473600L),
                        };
                        try
                        {
                            //CookieContainerに追加できないようなサイズの大きいvalueが存在したため、適合していることをチェックする。
                            //適合しなかったら例外が投げられ、追加しない。
                            cc.Add(cookie);
                            list.Add(cookie);
                        }
                        catch (CookieException) { }
                    }
                }
                return(list);
            }
Пример #38
0
 public HtmlDocument GetResponseHtml(string uri, Cookie sessionCookie = null, bool persistStatus200 = false)
 {
     return(getResponseHtml(uri, sessionCookie, persistStatus200));
 }
Пример #39
0
    private void BindData()
    {
        //获取用户信息
        Cookie cookie     = new Cookie();
        string taobaoNick = cookie.getCookie("nick");
        string session    = cookie.getCookie("top_session");

        Rijndael_ encode = new Rijndael_("tetesoft");

        taobaoNick = encode.Decrypt(taobaoNick);

        //判断是否删除
        string act = utils.NewRequest("act", utils.RequestType.QueryString);
        string id  = utils.NewRequest("id", utils.RequestType.QueryString);

        if (id != "" && !utils.IsInt32(id))
        {
            Response.Write("非法参数");
            Response.End();
            return;
        }

        if (act == "del")
        {
            string sql = "DELETE FROM TopIdea WHERE nick = '" + taobaoNick + "' AND id = " + id;
            utils.ExecuteNonQuery(sql);

            Response.Redirect("idealist.aspx");
            return;
        }
        else if (act == "update")
        {
            //如果是自动更新模式,需要更新店铺商品数据
            string    sql     = "SELECT shopcategoryid,query,nick,showtype FROM TopIdea WHERE nick = '" + taobaoNick + "' AND id = " + id;
            DataTable dt1     = utils.ExecuteDataTable(sql);
            string    shopcat = string.Empty;
            string    query   = string.Empty;
            if (dt1.Rows[0]["showtype"].ToString() == "0")
            {
                //更新下架商品数据
                shopcat    = dt1.Rows[0][0].ToString();
                query      = dt1.Rows[0][1].ToString();
                taobaoNick = dt1.Rows[0][2].ToString();

                //获取新商品列表
                TopXmlRestClient      client  = new TopXmlRestClient("http://gw.api.taobao.com/router/rest", "12132145", "1fdd2aadd5e2ac2909db2967cbb71e7f");
                ItemsOnsaleGetRequest request = new ItemsOnsaleGetRequest();
                request.Fields   = "num_iid,title,price,pic_url";
                request.PageSize = 12;
                request.OrderBy  = "list_time:desc";
                request.OrderBy  = "volume:desc";
                if (shopcat != "0")
                {
                    request.SellerCids = shopcat;
                }
                request.Q = query;

                //清理关联商品
                sql = "DELETE FROM TopIdeaProduct WHERE ideaid = " + id;
                utils.ExecuteNonQuery(sql);

                //未登录用户不能获取小二下架或删除的商品-错误过滤,原因未知
                try
                {
                    PageList <Item> product = client.ItemsOnsaleGet(request, session);

                    for (int i = 0; i < product.Content.Count; i++)
                    {
                        sql = "INSERT INTO TopIdeaProduct (" +
                              "itemid, " +
                              "itemname, " +
                              "itemprice, " +
                              "itempicurl, " +
                              "ideaid " +
                              " ) VALUES ( " +
                              " '" + product.Content[i].NumIid + "', " +
                              " '" + product.Content[i].Title + "', " +
                              " '" + product.Content[i].Price + "', " +
                              " '" + product.Content[i].PicUrl + "', " +
                              " '" + id + "' " +
                              ") ";
                        utils.ExecuteNonQuery(sql);
                    }
                }
                catch { }
            }

            //清理广告缓存
            CacheManager testcaching1 = CacheFactory.GetCacheManager();
            if (testcaching1.Contains("cache_1_" + id))
            {
                testcaching1.Remove("cache_1_" + id);
            }

            //更新广告图片
            string folderPath = Server.MapPath("\\show\\folder\\" + MD5(taobaoNick) + "\\result_" + id + ".jpg");
            if (File.Exists(folderPath))
            {
                File.Delete(folderPath);
            }

            //Response.Write("推广更新成功!!<br> <a href='../../show/plist.aspx?id=" + id + "' target='_blank'>查看更新过的广告</a> <br> <a href='idealist.aspx'>返回</a>");
            Response.Redirect("success.aspx?id=" + id);
            return;
        }

        DataTable dt = utils.ExecuteDataTable("SELECT * FROM TopIdea WHERE nick = '" + taobaoNick + "' ORDER BY id DESC");

        rptIdeaList.DataSource = dt;
        rptIdeaList.DataBind();
    }
Пример #40
0
 bool IResourceHandler.CanSetCookie(Cookie cookie) => true;
 /// <summary>
 /// Delete a cookie in the browser by passing in a copy of a cookie
 /// </summary>
 /// <param name="cookie">An object that represents a copy of the cookie that needs to be deleted</param>
 public void DeleteCookie(Cookie cookie)
 {
     _cookieJar.DeleteCookie(cookie);
 }
Пример #42
0
		/// <summary>
		/// Sets an HTTP cookie to be sent with all requests made with this FlurlClient.
		/// </summary>
		/// <param name="cookie">the cookie to set.</param>
		/// <returns>The modified FlurlClient.</returns>
		public static FlurlClient WithCookie(this FlurlClient client, Cookie cookie) {
			GetCookieContainer(client).Add(client.HttpClient.BaseAddress, cookie);
			return client;
		}
Пример #43
0
 public IRestClient AddCookie(Cookie cookie)
 {
     requestCookies.Add(cookie);
     return(this);
 }
Пример #44
0
		/// <summary>
		/// Creates a FlurlClient from the URL and sets an HTTP cookie to be sent with all requests made with this FlurlClient.
		/// </summary>
		/// <param name="cookie">the cookie to set.</param>
		/// <returns>The modified FlurlClient.</returns>
		public static FlurlClient WithCookie(this Url url, Cookie cookie) {
			return new FlurlClient(url, true).WithCookie(cookie);
		}
Пример #45
0
        public void TestDeep(Cookie cookie, SqlConnectionInfo sci)
        {
            Uri root = new Uri("http://localhost/DBlog/Default.aspx");

            List <Uri> queue   = new List <Uri>(2500);
            List <Uri> visited = new List <Uri>(5000);

            PerformanceDataCollection perfdata = (sci == null ? null : new PerformanceDataCollection());

            double totaltime  = 0;
            int    totalcount = 0;

            queue.Add(root);

            Dictionary <Uri, Uri> references = new Dictionary <Uri, Uri>();

            references.Add(root, new Uri("http://localhost/DBlog/"));

            while (queue.Count > 0)
            {
                Uri            topofqueue = queue[0];
                List <HtmlUri> links;
                double         ts = 0;

                PerformanceData perf = (perfdata != null ? new PerformanceData(topofqueue.ToString()) : null);
                try
                {
                    TraceServer traceServerReader = (sci == null ? null : SqlTrace.BeginTrace(sci));
                    TestPage(references[topofqueue], topofqueue, cookie, out links, out ts);
                    if (perfdata != null)
                    {
                        perfdata.Add(SqlTrace.EndTrace(topofqueue.ToString(), traceServerReader));
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine();
                    Console.WriteLine("{0}: {1}", topofqueue, ex.Message);
                    throw ex;
                }
                finally
                {
                    totalcount++;
                    totaltime += ts;
                    // add to visited links
                    visited.Add(topofqueue);
                    // remove from queue
                    queue.RemoveAt(0);
                }

                if (perfdata != null)
                {
                    perfdata.Add(perf);
                }

                // -----------------
                Console.Write("{0}/{1} [avg: {2}]", totalcount, queue.Count, (totaltime / totalcount).ToString("0.00"));
                if (perf != null)
                {
                    Console.Write("[SQL: {0} in {1}] ", perf.Queries, perf.TotalDuration);
                }
                Console.Write(" => ");

                int count = 0;
                foreach (HtmlUri uri in links)
                {
                    Uri fulluri = uri.Uri;

                    if (!root.IsBaseOf(fulluri))
                    {
                        // Console.WriteLine("\tSkipping {0}.", uri);
                        continue;
                    }

                    if (references.ContainsKey(fulluri) || queue.Contains(fulluri) || visited.Contains(fulluri))
                    {
                        continue;
                    }

                    Assert.IsFalse(fulluri.ToString().Contains("\0"),
                                   string.Format("Uri {0} in {1} contains non-ASCII character.", fulluri, topofqueue));

                    Assert.IsFalse(fulluri.ToString().Contains("<%"),
                                   string.Format("Uri {0} in {1} contains non-executed ASP.NET code.", fulluri, topofqueue));

                    Assert.IsFalse(fulluri.ToString().Contains("id=0"),
                                   string.Format("Uri {0} in {1} contains a link to a zero id.", fulluri, topofqueue));

                    references.Add(fulluri, topofqueue);

                    // Console.WriteLine("\tAdding {0}.", fulluri.OriginalString);
                    queue.Add(fulluri);
                    count++;
                }

                if ((perfdata != null) && (((totalcount > 0) && ((totalcount % 100) == 0)) || (queue.Count == 0)))
                {
                    perfdata.DumpPigs();
                }
            }
        }
Пример #46
0
        private async Task <ApiResponse <T> > ExecAsync <T>(RestRequest req, IReadableConfiguration configuration)
        {
            RestClient client = new RestClient(_baseUrl);

            client.ClearHandlers();
            var existingDeserializer = req.JsonSerializer as IDeserializer;

            if (existingDeserializer != null)
            {
                client.AddHandler(() => existingDeserializer, "application/json", "text/json", "text/x-json", "text/javascript", "*+json");
            }
            else
            {
                client.AddHandler(() => new CustomJsonCodec(configuration), "application/json", "text/json", "text/x-json", "text/javascript", "*+json");
            }

            client.AddHandler(() => new XmlDeserializer(), "application/xml", "text/xml", "*+xml", "*");

            client.Timeout = configuration.Timeout;

            if (configuration.UserAgent != null)
            {
                client.UserAgent = configuration.UserAgent;
            }

            if (configuration.ClientCertificates != null)
            {
                client.ClientCertificates = configuration.ClientCertificates;
            }

            InterceptRequest(req);

            var response = await client.ExecuteAsync <T>(req);

            InterceptResponse(req, response);

            var result = ToApiResponse(response);

            if (response.ErrorMessage != null)
            {
                result.ErrorText = response.ErrorMessage;
            }

            if (response.Cookies != null && response.Cookies.Count > 0)
            {
                if (result.Cookies == null)
                {
                    result.Cookies = new List <Cookie>();
                }
                foreach (var restResponseCookie in response.Cookies)
                {
                    var cookie = new Cookie(
                        restResponseCookie.Name,
                        restResponseCookie.Value,
                        restResponseCookie.Path,
                        restResponseCookie.Domain
                        )
                    {
                        Comment    = restResponseCookie.Comment,
                        CommentUri = restResponseCookie.CommentUri,
                        Discard    = restResponseCookie.Discard,
                        Expired    = restResponseCookie.Expired,
                        Expires    = restResponseCookie.Expires,
                        HttpOnly   = restResponseCookie.HttpOnly,
                        Port       = restResponseCookie.Port,
                        Secure     = restResponseCookie.Secure,
                        Version    = restResponseCookie.Version
                    };

                    result.Cookies.Add(cookie);
                }
            }
            return(result);
        }
Пример #47
0
        private async Task <ApiResponse <T> > ExecAsync <T>(RestRequest req, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            RestClient client = new RestClient(_baseUrl);

            client.ClearHandlers();
            var existingDeserializer = req.JsonSerializer as IDeserializer;

            if (existingDeserializer != null)
            {
                client.AddHandler("application/json", () => existingDeserializer);
                client.AddHandler("text/json", () => existingDeserializer);
                client.AddHandler("text/x-json", () => existingDeserializer);
                client.AddHandler("text/javascript", () => existingDeserializer);
                client.AddHandler("*+json", () => existingDeserializer);
            }
            else
            {
                var customDeserializer = new CustomJsonCodec(SerializerSettings, configuration);
                client.AddHandler("application/json", () => customDeserializer);
                client.AddHandler("text/json", () => customDeserializer);
                client.AddHandler("text/x-json", () => customDeserializer);
                client.AddHandler("text/javascript", () => customDeserializer);
                client.AddHandler("*+json", () => customDeserializer);
            }

            var xmlDeserializer = new XmlDeserializer();

            client.AddHandler("application/xml", () => xmlDeserializer);
            client.AddHandler("text/xml", () => xmlDeserializer);
            client.AddHandler("*+xml", () => xmlDeserializer);
            client.AddHandler("*", () => xmlDeserializer);

            client.Timeout = configuration.Timeout;

            if (configuration.Proxy != null)
            {
                client.Proxy = configuration.Proxy;
            }

            if (configuration.UserAgent != null)
            {
                client.UserAgent = configuration.UserAgent;
            }

            if (configuration.ClientCertificates != null)
            {
                client.ClientCertificates = configuration.ClientCertificates;
            }

            InterceptRequest(req);

            IRestResponse <T> response;

            if (RetryConfiguration.AsyncRetryPolicy != null)
            {
                var policy       = RetryConfiguration.AsyncRetryPolicy;
                var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellationToken).ConfigureAwait(false);

                response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize <T>(policyResult.Result) : new RestResponse <T>
                {
                    Request        = req,
                    ErrorException = policyResult.FinalException
                };
            }
            else
            {
                response = await client.ExecuteAsync <T>(req, cancellationToken).ConfigureAwait(false);
            }

            // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
            if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
            {
                response.Data = (T)typeof(T).GetMethod("FromJson").Invoke(null, new object[] { response.Content });
            }
            else if (typeof(T).Name == "Stream") // for binary response
            {
                response.Data = (T)(object)new MemoryStream(response.RawBytes);
            }

            InterceptResponse(req, response);

            var result = ToApiResponse(response);

            if (response.ErrorMessage != null)
            {
                result.ErrorText = response.ErrorMessage;
            }

            if (response.Cookies != null && response.Cookies.Count > 0)
            {
                if (result.Cookies == null)
                {
                    result.Cookies = new List <Cookie>();
                }
                foreach (var restResponseCookie in response.Cookies)
                {
                    var cookie = new Cookie(
                        restResponseCookie.Name,
                        restResponseCookie.Value,
                        restResponseCookie.Path,
                        restResponseCookie.Domain
                        )
                    {
                        Comment    = restResponseCookie.Comment,
                        CommentUri = restResponseCookie.CommentUri,
                        Discard    = restResponseCookie.Discard,
                        Expired    = restResponseCookie.Expired,
                        Expires    = restResponseCookie.Expires,
                        HttpOnly   = restResponseCookie.HttpOnly,
                        Port       = restResponseCookie.Port,
                        Secure     = restResponseCookie.Secure,
                        Version    = restResponseCookie.Version
                    };

                    result.Cookies.Add(cookie);
                }
            }
            return(result);
        }
Пример #48
0
        public Cookie GetCookie()
        {
            Cookie ck = new Cookie(this.Name, this.Value, this.Domain, this.Path, this.Expiry);

            return(ck);
        }
 /// <summary>
 /// Override built-in Cookies, return false to prevent the Cookie from being set.
 /// </summary>
 public virtual bool SetCookieFilter(IRequest req, Cookie cookie)
 {
     return(AllowSetCookie(req, cookie.Name));
 }
Пример #50
0
 public void SetCookie(Cookie cookie)
 {
 }
        /// <summary>
        /// Returns a session ID cookie given the specified username and password. Returns null if the login failed
        /// </summary>
        /// <param name="iBossURL"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        private Cookie GetLoginCookie()
        {
            try
            {
                if ((CachedLoginCookie == null) || DateTime.Now.Subtract(CachedLoginCookieTime) > new TimeSpan(2, 0, 0))
                {
                    string loginurl       = this.iBossURL + @"/ibreports/action/login";
                    string formParameters = string.Format("button=login&username={0}&password={1}", this.Username, this.Password);

                    // Send the login request to the login page
                    HttpWebRequest LoginRequest = (HttpWebRequest)WebRequest.Create(loginurl);
                    LoginRequest.UserAgent   = userAgent;
                    LoginRequest.ContentType = "application/x-www-form-urlencoded";
                    LoginRequest.Method      = "POST";
                    byte[] LoginBytes = Encoding.ASCII.GetBytes(formParameters);
                    LoginRequest.ContentLength = LoginBytes.Length;
                    using (Stream os = LoginRequest.GetRequestStream())
                    {
                        os.Write(LoginBytes, 0, LoginBytes.Length);
                    }
                    WebResponse LoginResponse = LoginRequest.GetResponse();

                    // Analyze the response - did we get a login page back, or did it redirect us?
                    Stream       receiveStream      = LoginResponse.GetResponseStream();
                    StreamReader readStream         = new StreamReader(receiveStream, Encoding.UTF8);
                    string       webresponseresults = readStream.ReadToEnd();

                    if (webresponseresults.Contains("Please login"))
                    {
                        // A login page was returned
                        return(null);
                    }
                    else
                    {
                        // A non-login page was returned
                        // Parse the returned cookie into a cookie object
                        string LoginCookieHeader = LoginResponse.Headers["Set-cookie"];;
                        Regex  pattern           = new Regex(@"JSESSIONID=(.+); Path=/ibreports");
                        Match  match             = pattern.Match(LoginCookieHeader);
                        string ParsedSessionID   = match.Groups[1].Value;
                        if (!string.IsNullOrEmpty(ParsedSessionID))
                        {
                            CachedLoginCookieTime = DateTime.Now;
                            CachedLoginCookie     = new Cookie("JSESSIONID", ParsedSessionID);
                            return(CachedLoginCookie);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }
                else
                {
                    return(CachedLoginCookie);
                }
            }
            catch
            {
                return(null);
            }
        }
Пример #52
0
        public string PostHtml()
        {
            Random Rdm = new Random();
            //产生0到100的随机数
            int iRdm = Rdm.Next(423456789, 499999999);

            string wxuin        = iRdm.ToString();
            string pass_ticket  = "1KLZo5j/85JCXGrbyj5vH6Wn2Ek1qDqjqj2U5tik1232P47mLxmwM+avvXgfWjy5";
            string appmsg_token = "954_1hcOzNorDwiokamoRrnyUm3rQ1fVwUQk-ZDN0s061MxSYtM-BH5313uQ0n5bDgdUat4FJSVA7RrhkCIN";

            string postData   = "action=vote&__biz=MzA4MDIzOTQ5OQ%3D%3D&uin=777&key=777&pass_ticket=" + pass_ticket + "&appmsg_token=" + appmsg_token + "&f=json&json=%7B%22super_vote_item%22%3A%5B%7B%22vote_id%22%3A495474521%2C%22item_idx_list%22%3A%7B%22item_idx%22%3A%5B%2216%22%5D%7D%7D%2C%7B%22vote_id%22%3A495474522%2C%22item_idx_list%22%3A%7B%22item_idx%22%3A%5B%2219%22%5D%7D%7D%5D%2C%22super_vote_id%22%3A495474497%7D&idx=2&mid=2653078119&wxtoken=777";
            string cookieStr  = "rewardsn=; wxuin=" + wxuin + "; devicetype=android-23; version=26060636; lang=zh_CN; pass_ticket=" + pass_ticket + "; wap_sid2=CLfb39UBElw3ZDlXaU5iNlVsYzB0UVlia3NvZktSWHpoM3FfVl9udFhBWlhJdlRrV0N4NVVwTUZ3V2ZCYW5aWUZrTkxMSVBZYlZyc2xUbTc0THZmWE16ZDNBWEkxYm9EQUFBfjCyyoTXBTgNQAE=; wxtokenkey=777";
            string RefererURl = "https://mp.weixin.qq.com/mp/newappmsgvote?action=show&__biz=MzA4MDIzOTQ5OQ==&supervoteid=495474497&uin=777&key=777&pass_ticket=" + pass_ticket + "&wxtoken=777&mid=2653078119&idx=2&appmsg_token=" + appmsg_token;
            string URL        = "https://mp.weixin.qq.com/mp/newappmsgvote";

            string[] cookstr = cookieStr.Split(';');
            foreach (string str in cookstr)
            {
                string[] cookieNameValue = str.Split('=');
                Cookie   ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
                ck.Domain = "mp.weixin.qq.com";
                _cc.Add(ck);
            }


            ASCIIEncoding encoding = new ASCIIEncoding();

            byte[] data = encoding.GetBytes(postData);//Encoding.UTF8.GetBytes(postData);

            HttpWebRequest  httpWebRequest;
            HttpWebResponse webResponse;
            Stream          getStream;
            StreamReader    streamReader;

            httpWebRequest               = (HttpWebRequest)HttpWebRequest.Create(URL);
            httpWebRequest.Host          = "mp.weixin.qq.com";
            httpWebRequest.ContentLength = data.Length;
            httpWebRequest.Accept        = "application/json";
            httpWebRequest.Headers.Add("Origin", "https://mp.weixin.qq.com");
            httpWebRequest.Headers.Add("X-Requested-With", "XMLHttpRequest");
            httpWebRequest.UserAgent   = "Mozilla/5.0 (Linux; Android 6.0; 1501-A02 Build/MRA58K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044028 Mobile Safari/537.36 MicroMessenger/6.6.6.1300(0x26060636) NetType/WIFI Language/zh_CN";
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.Referer     = RefererURl;
            httpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
            httpWebRequest.Headers.Add("Accept-Language", "zh-CN,en-US;q=0.8");
            httpWebRequest.Headers.Add("Q-UA2", "QV=3&PL=ADR&PR=WX&PP=com.tencent.mm&PPVN=6.6.6&TBSVC=43607&CO=BK&COVC=044028&PB=GE&VE=GA&DE=PHONE&CHID=0&LCID=9422&MO= 1501-A02 &RL=720*1280&OS=6.0&API=23");
            httpWebRequest.Headers.Add("Q-GUID", "04dd8ef186bdc34bdedac5a413b788cb");
            httpWebRequest.Headers.Add("Q-Auth", "31045b957cf33acf31e40be2f3e71c5217597676a9729f1b");
            httpWebRequest.CookieContainer = _cc;
            httpWebRequest.Method          = "POST";



            // httpWebRequest.AllowAutoRedirect = true;
            Stream pReqStream = httpWebRequest.GetRequestStream();

            // Send the data.
            pReqStream.Write(data, 0, data.Length);
            pReqStream.Close();

            webResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            getStream    = webResponse.GetResponseStream();
            streamReader = new StreamReader(getStream, Encoding.UTF8);
            string getString = "";

            getString = streamReader.ReadToEnd();

            streamReader.Close();
            getStream.Close();
            webResponse.Close();

            return(getString);
        }
Пример #53
0
        public void TestPage(Uri root, Uri uri, Cookie cookie, out List <HtmlUri> links, out double ts)
        {
            DateTime start = DateTime.UtcNow;

            Console.Write("{0} (from {1}) ...", uri.PathAndQuery, root.PathAndQuery);

            CookieContainer cookies = null;

            if (cookie != null)
            {
                cookies = new CookieContainer();
                cookies.Add(cookie);
            }

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);

            if (cookies != null)
            {
                request.CookieContainer = cookies;
            }
            request.Method            = "GET";
            request.AllowAutoRedirect = true;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream          s        = response.GetResponseStream();
            StreamReader    sr       = new StreamReader(s);

            string data = sr.ReadToEnd();

            Assert.IsTrue(response.StatusCode == HttpStatusCode.OK, string.Format("Response code was {0}", response.StatusCode));
            Assert.IsFalse(string.IsNullOrEmpty(data));

            ts = DateTime.UtcNow.Subtract(start).TotalSeconds;
            Console.Write("[{0} bytes][{1} sec.]", data.Length, ts.ToString("0.00"));

            Assert.IsTrue(data.Length > 0, "Downloaded file size is zero.");

            if (response.ContentType != "text/html; charset=utf-8")
            {
                Console.WriteLine("[ignore: {0}]", response.ContentType);
                links = new List <HtmlUri>();
                return;
            }

            // login page
            if (response.ResponseUri.PathAndQuery.Contains("DBlog/Login.aspx") && response.ResponseUri.PathAndQuery.Contains("access=denied"))
            {
                Console.WriteLine("[ignore: {0}]", uri.PathAndQuery);
                links = new List <HtmlUri>();
                return;
            }

            int error_start = data.IndexOf("<div id=\"ctl00_noticeMenu_panelNotice\" class=\"notice_error\"");

            if (error_start > 0)
            {
                int    error_end = data.IndexOf("</div>", error_start);
                string error     = data.Substring(error_start, error_end - error_start);
                Assert.IsTrue(error_start < 0, string.Format("{0}: error: {1}", uri, error));
            }

            links = HtmlUriExtractor.Extract(data, root);
            // Console.WriteLine("\tExtracted {0} links.", links.Count);

            Console.WriteLine("[done]");
        }
Пример #54
0
        static void Main(string[] args)
        {
            var urls = new List <string>();

            for (int i = 1; i < 9; i++)
            {
                string index = i.ToString();
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
                HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(link);
                webreq.UserAgent   = "Mozilla/5.0 (Windows NT 6.1; Win64;x64; rv: 58.0) Gecko / 20100101 Firefox / 58.0";
                webreq.ContentType = "application/x-www-form-urlencoded;charset = UTF - 8";
                //webreq.MediaType = "application/json";
                webreq.Accept = "application/json, text/javascript, /; q=0.01";

                webreq.Method = "POST";

                CookieContainer container = new CookieContainer();
                Cookie          ck        = new Cookie();
                webreq.Headers.Add("Cookie",
                                   @"forty_n_user=AdtdiU80-T3GEVr4aAuQAA~~;forty_n_t=1.cf026a.1534782833.1.4.1534782833.1534782948.4.0;di_roxanne[visit_id]=453190142; di_roxannevisitor_id]=714806210;diGeolocationIgnoreData={""currentRequestCount"":2,""createdAt"":1534782850335};tp_referral=1541655;di_referral=%7B%22ppc_terms%22%3Anull%2C%22url%22%3A%22NA%22%2C%22type%22%3A%22Direct%22%2C%22createdAt%22%3A%222018-08-20T16%3A33%3A55%2B0000%22%7D;di_page_history=a%3A2%3A%7Bi%3A0%3Bs%3A13%3A%22used-vehicles%22%3Bi%3A1%3Bs%3A28%3A%22welcome-to-lexus-of-edmonton%22%3B%7D;di_visited=3; m_sidebar_user_settings_used=type;gwcc=%7B%22expires%22%3A86400%2C%22backoff_expires%22%3A1534869246%7D;im-visitor-ip=23.17.106.204");
                string postData = string.Format(@"action=im_ajax_call&perform=get_results&order=t3j5n5&orderby=distance&page={0}&_nonce=fb607d6b7586e676a3010da691f417b7&_post_id={1}&_referer=/used-vehicles/", index, "5");



                webreq.ContentLength = postData.Length;
                using (Stream writeStream = webreq.GetRequestStream())
                {
                    UTF8Encoding encoding = new UTF8Encoding();
                    byte[]       bytes    = encoding.GetBytes(postData);
                    writeStream.Write(bytes, 0, bytes.Length);
                }

                string outputstring = string.Empty;
                using (HttpWebResponse response = (HttpWebResponse)webreq.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader readStream = new
                                                         StreamReader(responseStream, Encoding.UTF8))
                        {
                            outputstring = readStream.ReadToEnd();
                        }
                    }
                }
                HtmlDocument document = new HtmlDocument();

                document.LoadHtml(outputstring);
                var cars = document.DocumentNode.SelectNodes(".//div[contains(@class,'vehicle-title')]");
                foreach (var car in cars)
                {
                    var carDetail = car.SelectSingleNode(".//a");
                    var url       = carDetail.GetAttributeValue("href", "").Replace("\\\"", "").Replace("/", "");
                    if (!urls.Contains(url))
                    {
                        urls.Add(url);
                    }
                }
            }

            Console.ReadKey();
        }
Пример #55
0
 public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority)
 {
     authCookie = null;
     user       = password = authority = null;
     return(false);
 }
Пример #56
0
        public static string HttpPost(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

            request.Method        = "POST";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = postDataStr.Length;
            request.KeepAlive     = true;
            if (cc != null)
            {
                request.CookieContainer = cc;
            }

            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";
            StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);

            writer.Write(postDataStr);
            writer.Flush();
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return(response.StatusCode.ToString());
                }
                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8"; //默认编码
                }

                string[] cookies = response.Headers.GetValues("Set-Cookie");
                if (cookies != null && cookies.Length > 0)
                {
                    foreach (string cookie in cookies)
                    {
                        string[] cs = cookie.Split(';');
                        foreach (string ca in cs)
                        {
                            string[] kv = ca.Split('=');
                            if (kv.Length == 2)
                            {
                                Cookie c = new Cookie(kv[0].TrimStart(' '), kv[1], "/", "english.ulearning.cn");
                                if (cc == null)
                                {
                                    cc = new CookieContainer();
                                }
                                cc.Add(c);
                            }
                        }
                    }
                }
                StreamReader reader    = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                string       retString = reader.ReadToEnd();
                return(retString);
            }
            catch
            {
                return("");
            }
        }
Пример #57
0
 public void TestDeep(Cookie cookie)
 {
     TestDeep(cookie, null);
 }
Пример #58
0
        public void SetCookie(string name, string value, string domain)
        {
            var cookie = new Cookie(name, value, domain, "/", null);

            _browser.Manage().Cookies.AddCookie(cookie);
        }
Пример #59
0
        public void TestDeepPerformance(Cookie cookie)
        {
            SqlConnectionInfo sci = SqlTrace.GetTraceSqlConnectionInfo();

            TestDeep(cookie, sci);
        }
 /// <summary>
 /// Method for creating a cookie in the browser
 /// </summary>
 /// <param name="cookie"><see cref="Cookie"/> that represents a cookie in the browser</param>
 public void AddCookie(Cookie cookie)
 {
     _cookieJar.AddCookie(cookie);
 }