Ejemplo n.º 1
0
        /// <summary>
        /// create an instance
        /// </summary>
        /// <returns></returns>
        private SeleniumDriverInfo CreateInstance()
        {
            try
            {
                SeleniumDriverInfo seleniumDriverInfo = new SeleniumDriverInfo();

                seleniumDriverInfo.lastActiveTime = DateTime.Now;
                seleniumDriverInfo.hashId         = Guid.NewGuid().ToString();

                var options = new ChromeOptions();
                options.AddArgument("--window-position=-32000,-32000");
                options.AddArgument("headless");
                options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
                options.AddUserProfilePreference("profile.default_content_settings.popups", 0);

                ChromeDriver instance = new ChromeDriver(GetChromeDriverService(), options);
                seleniumDriverInfo.client = instance;

                return(seleniumDriverInfo);
            }
            catch (Exception ex)
            {
                Logger.Error("打开浏览器错误:" + ex.ToString());
            }
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取一个SeleniumDriver
        /// </summary>
        /// <returns></returns>
        public SeleniumDriverInfo BorrowInstance()
        {
Selenium:

            if (activedClients.Count > 0)
            {
                var delayKeys = activedClients.Where(f => f.Value.delayTime <= DateTime.Now).Select(f => f.Key);
                foreach (string delayKey in delayKeys)
                {
                    SeleniumDriverInfo clientInfo;
                    if (activedClients.TryRemove(delayKey, out clientInfo))
                    {
                        clientInfo.Dispose();
                    }
                }
            }

            if (objectPool.Count == 0)
            {
                if (activedClients.Count >= browserClientCount)
                {
                    Logger.Warn("SeleniumDriverInfo pool is full.");
                    resetEvent.WaitOne();
                }
                else
                {
                    PushObject(CreateInstance());
                }
            }

            SeleniumDriverInfo seleniumClient = null;
            bool result = objectPool.TryPop(out seleniumClient);

            if (!result)
            {
                goto Selenium;
            }

            //每借出一个client均需要将它缓存,并根据配置标识其最后超时时间
            seleniumClient.lastActiveTime = DateTime.Now;
            seleniumClient.delayTime      = seleniumClient.lastActiveTime.AddHours(3);

            activedClients[seleniumClient.hashId] = seleniumClient;

            return(seleniumClient);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 还回一个链接
        /// </summary>
        /// <param name="instance"></param>
        public void ReturnInstance(SeleniumDriverInfo instance)
        {
            if (objectPool.Count >= browserClientCount)
            {
                DestoryInstance(instance);
            }
            else
            {
                //归还后需要移除掉缓存中的部分
                SeleniumDriverInfo clientInfo;
                activedClients.TryRemove(instance.hashId, out clientInfo);
                clientInfo.client.SwitchTo().Window(clientInfo.client.WindowHandles[1]).Close();
                clientInfo.client.SwitchTo().Window(clientInfo.client.WindowHandles[0]);

                PushObject(instance);

                resetEvent.Set();
            }
        }
Ejemplo n.º 4
0
        public static HttpResult HttpRequest(string url, string postData = null, CookieContainer cookies = null, string userAgent = null, string referer = null, string cookiesDomain = null, Encoding encode = null, string method = null, IWebProxy proxy = null, string encoding = null, string contentType = null, int timeout = 8000, Dictionary <string, string> headers = null)
        {
            HttpResult httpResponse = new HttpResult();

            SeleniumDriverInfo seleniumDriverInfo = null;

            try
            {
                seleniumDriverInfo = SeleniumPool.Instance.BorrowInstance();

                // open a new tab and set the context
                ((ChromeDriver)seleniumDriverInfo.client).ExecuteScript("window.open('_blank', 'worktab');");
                seleniumDriverInfo.client.SwitchTo().Window("worktab");
                seleniumDriverInfo.client.Navigate().GoToUrl(url);
                Thread.Sleep(3000); //执行成功后仍然等待3S,用于接收后加载数据

                httpResponse.Url          = seleniumDriverInfo.client.Url;
                httpResponse.HttpCode     = 200; //todo, 根据返回的内容解析错误代码
                httpResponse.LastModified = TimeUtility.ConvertDateTimeInt(DateTime.Now);

                httpResponse.Content = seleniumDriverInfo.client.PageSource;
            }
            catch (Exception ex)
            {
                httpResponse.Url      = url;
                httpResponse.HttpCode = 500;
            }
            finally
            {
                if (seleniumDriverInfo != null)
                {
                    SeleniumPool.Instance.ReturnInstance(seleniumDriverInfo);
                }
            }

            return(httpResponse);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// destory the instance.
 /// </summary>
 /// <param name="instance"></param>
 private void DestoryInstance(SeleniumDriverInfo instance)
 {
     instance.Dispose();
 }
Ejemplo n.º 6
0
 /// <summary>
 /// push an instance to object pool.
 /// </summary>
 /// <param name="transport"></param>
 private void PushObject(SeleniumDriverInfo transport)
 {
     objectPool.Push(transport);
 }