Example #1
0
        private async void Btn_Surfing_Click(object sender, RoutedEventArgs e)
        {
            ScrollToEnd();

            var url = this.tbox_Url.Text.Trim();

            if (string.IsNullOrEmpty(url))
            {
                EMessageBox.Show("请输入要抓取的电商类网址");
            }

            AppendText($"正在从{url}获取........");

            var source = await WebUtil.GetDynamicHtmlSourceWithChromium(this, url);

            AppendText("获取页面完成,正在分析");

            var good = GetGood(source);

            if (good == null)
            {
                AppendText($"分析失败,{url}可能不是一个商品详情页地址");
            }
            else
            {
                AppendText("分析完成,结果如下.");
                ShowResult(good);
            }
        }
        private void btn_MultiThreadDonwload_Click(object sender, RoutedEventArgs e)
        {
            string url = this.tbox_Url.Text.Trim();

            if (string.IsNullOrEmpty(url))
            {
                EMessageBox.Show("请输入下载地址");
                return;
            }

            if (globalData.CrawlerConfig.ImageConfig.IgnoreUrlCheck == false)
            {
                //TODO
                //文件URL检验
            }

            ShowStatusText($"check {url} status");
            HttpHeader httpHeader = WebUtil.GetHeader(url);

            if (httpHeader.StatusCode != System.Net.HttpStatusCode.OK)
            {
                ShowStatusText($"{url} is not available");
                return;
            }

            ShowStatusText($"{url} is available");
            ShowStatusText($"Download file {url}.....");

            WebUtil.DownloadFileWithProgress(url, ShowStatusText);
        }
Example #3
0
        private async void btn_Fetch_Click(object sender, RoutedEventArgs e)
        {
            var url = this.tbox_Url.Text.Trim();

            if (string.IsNullOrEmpty(url))
            {
                EMessageBox.Show("请输入网址");
                return;
            }

            var robotsUrl = url.EndsWith("/") ? url + "robots.txt" : url + "/robots.txt";

            robotsUrl = UrlUtil.FixUrl(robotsUrl);

            if (WebUtil.IsResourceAvailable(robotsUrl) == false)
            {
                this.lbl_Result.Text = "该网站没有爬虫协议";
                return;
            }

            var stream = await WebUtil.GetHtmlStreamAsync(robotsUrl);

            var result = WebUtil.ResolveRobotsProtocol(stream);

            stream.Close();

            //显示结果
            this.lbl_Result.Text = $"从【{url}】获取到的爬虫协议如下(目录以;分隔)\r\n\n";
            foreach (var item in result)
            {
                this.lbl_Result.Text += item.ToString();
            }
        }
        private void btn_Fetch_Click(object sender, RoutedEventArgs e)
        {
            string   url      = this.tbox_Url.Text;
            Encoding encoding = GetChoosedEncoding();

            if (UrlUtil.IsEmpty(url))
            {
                EMessageBox.Show("请输入网址");
                this.tbox_Url.Focus();
                return;
            }

            bool isStartWithHttp = false;

            if (RegexUtil.IsUrl(url, out isStartWithHttp) == false)
            {
                EMessageBox.Show("网址输入错误");
                this.tbox_Url.Focus();
                return;
            }

            ClearControls();

            GetHostIP(url, isStartWithHttp);
            GetHeaderInfo(url, isStartWithHttp);
            GetHtmlSource(url, isStartWithHttp, encoding);
        }
        /// <summary>
        /// 在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Query_Click(object sender, RoutedEventArgs e)
        {
            TextRange tr = new TextRange(rbox_Input.Document.ContentStart, rbox_Input.Document.ContentEnd);

            var selector = this.tbox_CSSSelector.Text.Trim();
            var html     = tr.Text;

            if (string.IsNullOrEmpty(selector))
            {
                EMessageBox.Show("请输入CSS选择器");
                return;
            }

            var angleSharpObj = AngleSharpHelper.GetInstance(html).CSSQuery(selector);

            if (angleSharpObj != null)
            {
                //目前只输出一个结果
                //过完年再搞 ╮(-_-)╭
                this.rbox_Output.Document = new FlowDocument(new Paragraph(new Run(angleSharpObj.OuterHtml)));
            }
            else
            {
                this.rbox_Output.Document = new FlowDocument(new Paragraph(new Run("未匹配到结果")));
            }
        }
        /// <summary>
        /// XPath 是一门在 XML 文档中查找信息的语言。XPath 用于在 XML 文档中通过元素和属性进行导航。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_XPathQuery_Click(object sender, RoutedEventArgs e)
        {
            TextRange tr   = new TextRange(rbox_XPathInput.Document.ContentStart, rbox_XPathInput.Document.ContentEnd);
            var       html = tr.Text;

            if (string.IsNullOrEmpty(html))
            {
                EMessageBox.Show("请输入html");
                return;
            }

            var result = HtmlAgilityPackUtil.XPathQuery(html, this.tbox_XPath.Text.Trim());

            if (result != null)
            {
                Paragraph paragraph = new Paragraph();
                foreach (var item in result)
                {
                    paragraph.Inlines.Add(new Run(item.OuterHtml + Environment.NewLine));
                }
                this.rbox_XPathOutput.Document = new FlowDocument(paragraph);
            }
            else
            {
                this.rbox_XPathOutput.Document.Blocks.Clear();
            }
        }
Example #7
0
        private async void btn_Download_Click(object sender, RoutedEventArgs e)
        {
            if (imageCollection.Count == 0)
            {
                EMessageBox.Show("请先输入网址,执行Surfing获取到图片后再单击下载");
                return;
            }

            var localFileList = await DownloadImage(imageCollection);

            if (cbox_Filter.IsChecked.Value == true)
            {
                if (string.IsNullOrEmpty(tbox_Height.Text) || string.IsNullOrEmpty(tbox_Width.Text))
                {
                    EMessageBox.Show("请输入要过滤的宽高");
                    return;
                }

                //过滤
                var width  = 1024;
                var height = 768;
                int.TryParse(this.tbox_Width.Text, out width);
                int.TryParse(this.tbox_Height.Text, out height);

                FilterImage(localFileList, width, height);
            }
        }
Example #8
0
        private async void btn_Fetch_Click(object sender, RoutedEventArgs e)
        {
            var url = this.tbox_Url.Text.Trim();

            if (string.IsNullOrEmpty(url))
            {
                EMessageBox.Show("请输入网址");
                return;
            }

            try
            {
                var source = await WebUtil.HttpClientGetStringAsync(url);

                AppendText("开始获取链接");
                await ExtractLink(source);

                AppendText("获取链接完成,开始多线程抓取链接内容......");
                await ExtractTitleMultiThread();

                AppendText("获取链接标题完成");
            }
            catch (Exception ex)
            {
                AppendText(ex.Message);
            }
        }
        private async void btn_HttpClientFetch_Click(object sender, RoutedEventArgs e)
        {
            var url = this.tbox_HttpClientUrl.Text.Trim();

            if (string.IsNullOrEmpty(url))
            {
                EMessageBox.Show("请输入网址");
                return;
            }

            if (url.StartsWith("http") == false && url.StartsWith("https") == false)
            {
                url = "http://" + url;
            }

            this.stackpaneHttpClient.Children.Clear();

            Encoding encoding = null;

            if (combox_HttpClientEncoding.SelectedIndex > 0)
            {
                encoding = Encoding.GetEncoding(combox_HttpClientEncoding.Text);
            }

            var html = await WebUtil.HttpClientGetStringAsync(url, encoding : encoding);

            RichTextBox richTextBox = new RichTextBox();

            richTextBox.Height = this.stackpanel.ActualHeight - 180;
            richTextBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            richTextBox.Document = new FlowDocument(new Paragraph(new Run(html)));
            this.stackpaneHttpClient.Children.Add(richTextBox);
        }
Example #10
0
        private async void btn_Login_Selenium_Click(object sender, RoutedEventArgs e)
        {
            var userName = this.tbox_UserName_Selenium.Text.Trim();
            var password = this.tbox_Password_Selenium.Text.Trim();

            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                EMessageBox.Show("请输入用户名或密码");
                return;
            }

            using (OpenQA.Selenium.IWebDriver driver = new OpenQA.Selenium.Edge.EdgeDriver())
            {
                driver.Navigate().GoToUrl("http://i.360.cn");  //driver.Url = "http://i.360.cn"是一样的

                var source = driver.PageSource;

                this.rtbox_BeforeLoginContent_Selenium.Document = new FlowDocument(new Paragraph(new Run(source)));

                //这个等待是无效的,只是测试代码,可以直接启用下载获取username的代码
                QA.Support.UI.WebDriverWait wait = new QA.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(2));
                QA.IWebElement userNameEle       = wait.Until <QA.IWebElement>(d => d.FindElement(QA.By.Name("userName")));

                //QA.IWebElement userNameEle = driver.FindElement(QA.By.Name("userName"));
                QA.IWebElement passwordEle = driver.FindElement(QA.By.Name("password"));
                QA.IWebElement loginEle    = driver.FindElement(QA.By.ClassName("quc-button-submit quc-button quc-button-primary"));

                //填写用户名
                userNameEle.SendKeys(QA.Keys.Tab);
                userNameEle.Clear();
                userNameEle.SendKeys(userName);
                //主动等待2秒
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);

                //填写密码
                passwordEle.SendKeys(QA.Keys.Tab);
                passwordEle.Clear();
                passwordEle.SendKeys(password);
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);

                //点击登录按钮
                loginEle.Click();

                //主动等待5秒
                //使用driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);这种方式等待无效
                //登录需要时间,如果直接去获取Cookie,获取的还是未登录前的Cookie
                System.Threading.Thread.Sleep(5000);

                //Cookies
                var cookies = driver.Manage().Cookies.AllCookies;

                var cookieContainer = SeleniumUtil.CookieConvert(cookies);

                var html = await WebUtil.GetHtmlSource("http://i.360.cn", cookieContainer : cookieContainer);

                this.rtbox_AfterLoginContent_Selenium.Document = new FlowDocument(new Paragraph(new Run(html.Item1)));
            }
        }
        private async void btn_SaveAsPDF_Click(object sender, RoutedEventArgs e)
        {
            //打开网页的操作跟上面是一样的
            try
            {
                await new PuppeteerSharp.BrowserFetcher().DownloadAsync(PuppeteerSharp.BrowserFetcher.DefaultRevision);
                var browser = await PuppeteerSharp.Puppeteer.LaunchAsync(new PuppeteerSharp.LaunchOptions
                {
                    Headless = true
                });

                var page = await browser.NewPageAsync();  //打开一个新标签

                await page.GoToAsync(this.tbox_Url.Text); //访问页面

                //设置PDF选项
                PuppeteerSharp.PdfOptions pdfOptions = new PuppeteerSharp.PdfOptions();
                pdfOptions.DisplayHeaderFooter = false; //是否显示页眉页脚
                pdfOptions.FooterTemplate      = "";    //页脚文本

                var width = await page.EvaluateFunctionAsync <int>("function getWidth(){return document.body.scrollWidth}");

                var height = await page.EvaluateFunctionAsync <int>("function getHeight(){return document.body.scrollHeight}");

                pdfOptions.Width  = $"{width}px";
                pdfOptions.Height = $"{height}px";

                pdfOptions.HeaderTemplate = "";    //页眉文本
                pdfOptions.Landscape      = false; //纸张方向 false-垂直 true-水平
                pdfOptions.MarginOptions  = new PuppeteerSharp.Media.MarginOptions()
                {
                    Bottom = "0px", Left = "0px", Right = "0px", Top = "0px"
                };                                //纸张边距,需要设置带单位的值,默认值是None
                pdfOptions.Scale           = 1m;  //PDF缩放,从0-1
                pdfOptions.PrintBackground = true;

                var fileName = Environment.CurrentDirectory + $"\\download\\{await page.GetTitleAsync()}.pdf";

                if (System.IO.File.Exists(fileName))
                {
                    fileName = fileName.Replace(".pdf", $"{DateTime.Now.ToString("ffff")}.pdf");
                }

                //保存PDF
                await page.PdfAsync(fileName, pdfOptions);

                EMessageBox.Show($"{fileName}保存成功");

                //在最后记得关闭浏览器及释放资源
                browser.Disconnect();
                browser.Dispose();
            }
            catch (Exception ex)
            {
                EMessageBox.Show(ex.Message);
            }
        }
Example #12
0
        private async void btn_Login_Click(object sender, RoutedEventArgs e)
        {
            var userName = this.tbox_UserName.Text.Trim();
            var password = this.tbox_Password.Text.Trim();

            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                EMessageBox.Show("请输入用户名或密码");
                return;
            }
            try
            {
                var sourceBeforeLogin = await WebUtil.GetHtmlSource(UrlUtil._360UserHome);

                this.rtbox_BeforeLoginContent.Document = new FlowDocument(new Paragraph(new Run(sourceBeforeLogin)));

                //获取登录Token
                var getTokenUrl = $"https://login.360.cn/?func=jQuery112406519812780967824_1569420309197&src=pcw_i360&from=pcw_i360&charset=UTF-8&requestScema=https&quc_sdk_version=6.8.3&quc_sdk_name=jssdk&o=sso&m=getToken&userName={userName}&_=1569420309199";

                //请求结果如下:

                /*
                 *  jQuery112406519812780967824_1569420309197({"errno":0,"errmsg":"","token":"916e4fcbfa1c06e5"})
                 *  就不转成对象了,直接使用正则
                 */

                //需要设置Cookie
                CookieContainer cookieContainer = new CookieContainer();
                Cookie          _DC_gidCookie   = new Cookie("__DC_gid", "59612149.266039479.1569828952751.1569829055668.4");
                _DC_gidCookie.Domain = "login.360.cn";
                cookieContainer.Add(_DC_gidCookie);
                Cookie _guidCookie = new Cookie("__guid", "59612149.2764161016782091300.1569828952751.616");
                _guidCookie.Domain = "login.360.cn";
                cookieContainer.Add(_guidCookie);

                //向服务器请求登录
                var requesttLoginUrl = $"http://s.360.cn/i360/qhpass.htm?src=pcw_i360&version=6.8.3&guid={_guidCookie.Value}&action=submit&module=signin";
                await WebUtil.GetHtmlSource(requesttLoginUrl, cookieContainer : cookieContainer);

                var sourceGetToken = await WebUtil.GetHtmlSource(getTokenUrl, cookieContainer : cookieContainer);

                var token = RegexUtil.Match(sourceGetToken.Item1, RegexPattern.Get360TokenPattern).Groups["token"].Value;

                password = EncryptionUtil.MD5_32(password);
                var postData         = $"src=pcw_i360&from=pcw_i360&charset=UTF-8&requestScema=https&quc_sdk_version=6.8.3&quc_sdk_name=jssdk&o=sso&m=login&lm=0&captFlag=1&rtype=data&validatelm=0&isKeepAlive=1&captchaApp=i360&userName={userName}&smDeviceId=&type=normal&account={userName}&password={password}&captcha=&token={token}&proxy=http%3A%2F%2Fi.360.cn%2Fpsp_jump.html&callback=QiUserJsonp331446237&func=QiUserJsonp331446237";
                var contentType      = "application/x-www-form-urlencoded";
                var sourceAfterLogin = await WebUtil.PostData(UrlUtil._360LoginUrl, postData, contentType, cookieContainer);

                this.rtbox_AfterLoginContent.Document = new FlowDocument(new Paragraph(new Run(sourceAfterLogin)));
                ShowStatusMessage("登录成功");
            }
            catch (Exception ex)
            {
                ShowStatusMessage("登录失败-" + ex.Message);
            }
        }
        private void btn_ReadStringFromBDB_Click(object sender, RoutedEventArgs e)
        {
            var guid = this.tbox_Key.Text;
            var key  = new DatabaseEntry();

            key.Data = Encoding.ASCII.GetBytes(guid);
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair = bdb.Get(key);

            EMessageBox.Show(Encoding.ASCII.GetString(pair.Value.Data));
        }
        private async void btn_SaveAsImage_Click(object sender, RoutedEventArgs e)
        {
            //如果这一步发生异常了,可以参考 https://www.cnblogs.com/zhaotianff/p/13528507.html
            try
            {
                await new PuppeteerSharp.BrowserFetcher().DownloadAsync(PuppeteerSharp.BrowserFetcher.DefaultRevision);

                var browser = await PuppeteerSharp.Puppeteer.LaunchAsync(new PuppeteerSharp.LaunchOptions
                {
                    Headless = true
                });

                var page = await browser.NewPageAsync();  //打开一个新标签

                await page.GoToAsync(this.tbox_Url.Text); //访问页面

                //设置截图选项
                PuppeteerSharp.ScreenshotOptions screenshotOptions = new PuppeteerSharp.ScreenshotOptions();
                //screenshotOptions.Clip = new PuppeteerSharp.Media.Clip() { Height = 0, Width = 0, X = 0, Y = 0 };//设置截剪区域
                screenshotOptions.FullPage       = true;                               //是否截取整个页面
                screenshotOptions.OmitBackground = false;                              //是否使用透明背景,而不是默认白色背景
                screenshotOptions.Quality        = 100;                                //截图质量 0-100(png不可用)
                screenshotOptions.Type           = PuppeteerSharp.ScreenshotType.Jpeg; //截图格式

                var fileName = Environment.CurrentDirectory + $"\\download\\{await page.GetTitleAsync()}.jpg";

                if (System.IO.File.Exists(fileName))
                {
                    fileName = fileName.Replace(".jpg", $"{DateTime.Now.ToString("ffff")}.jpg");
                }

                await page.ScreenshotAsync(fileName, screenshotOptions);

                if (System.IO.File.Exists(fileName))
                {
                    BitmapImage bi = new BitmapImage();
                    bi.BeginInit();
                    bi.UriSource = new Uri(fileName, UriKind.Absolute);
                    bi.EndInit();
                    this.image.Source = bi;
                }
                else
                {
                    EMessageBox.Show("保存网页截图失败");
                }

                //在最后记得关闭浏览器及释放资源
                browser.Disconnect();
                browser.Dispose();
            }
            catch (Exception ex)
            {
                EMessageBox.Show(ex.Message);
            }
        }
Example #15
0
        private void btn_Fetch_Click(object sender, RoutedEventArgs e)
        {
            string url = this.tbox_Url.Text.Trim();

            if (string.IsNullOrEmpty(url))
            {
                EMessageBox.Show("请输入网址");
                return;
            }

            OpenUrlWithChromium(url);
        }
        private void btn_ReadObjectFromBDB_Click(object sender, RoutedEventArgs e)
        {
            var key = new DatabaseEntry();

            key.Data = Encoding.ASCII.GetBytes("TestClassKey");
            CrawlerRecord record = bdb.Get <CrawlerRecord>(key);

            if (record != null)
            {
                EMessageBox.Show($"ID = {record.id},Url = {record.url},Title = {record.title},Content = {record.content}");
            }
        }
        private void btn_WriteStringToBDB_Click(object sender, RoutedEventArgs e)
        {
            string        guid = this.tbox_Key.Text;
            string        url  = this.tbox_Value.Text;
            DatabaseEntry key  = new DatabaseEntry();

            key.Data = Encoding.ASCII.GetBytes(guid);
            DatabaseEntry value = new DatabaseEntry();

            value.Data = Encoding.ASCII.GetBytes(url);
            bdb.Put(key, value);
            EMessageBox.Show("写入成功");
        }
Example #18
0
        /*
         * 像知乎,微博这种页面,页面需要滑动到最底部才会去加载动态内容。如果去分析接口,费时费力,还得每个网站都分析一次。
         * 个人觉得,可以控制浏览器一直滚动到最底部,待动态内容全部加载完成后,再进行抓取。这样就省了分析接口这一步,而且一劳永逸。
         * 如果需要登录的,可以进行模拟登录,再进行抓取(一些网站只需关闭弹出的登录对话框,就可以直接抓取)
         */


        private void btn_SurfingDynamic_Click(object sender, RoutedEventArgs e)
        {
            //下面开始操作
            var url = this.tbox_UrlDynamic.Text.Trim();

            if (string.IsNullOrEmpty(url))
            {
                EMessageBox.Show("请输入网址");
                return;
            }

            globalData.Browser.GetHtmlSourceDynamic(url, StartScroll);
        }
        private void btn_PageDown_Click(object sender, RoutedEventArgs e)
        {
            if (globalData.CrawlerConfig.ImageConfig.PageDownRule == -1)
            {
                EMessageBox.Show("未配置翻页规则,无法跳转到下一页");
                return;
            }

            var urlArray = globalData.CrawlerConfig.ImageConfig.PageDownUrl.Split(';');
            var url      = UrlUtil.GetPageDownUrl(Page++, urlArray[0], urlArray[1]);

            this.tbox_Url.Text = url;
            btn_Surfing_Click(null, null);
        }
Example #20
0
        private async void btn_Search_Click(object sender, RoutedEventArgs e)
        {
            List <TagImg> searchResult = new List <TagImg>();
            string        keyword      = this.tbox_Keyword.Text.Trim();

            if (string.IsNullOrEmpty(keyword))
            {
                EMessageBox.Show("请输入搜索关键字");
                return;
            }

            searchResult = await SearchBingImage(keyword);

            ShowImage(searchResult);
        }
        private void btn_WriteObjectToBDB_Click(object sender, RoutedEventArgs e)
        {
            var key = new DatabaseEntry();

            key.Data = Encoding.ASCII.GetBytes("TestClassKey");
            CrawlerRecord crawlerRecord = new CrawlerRecord()
            {
                id = 1, content = "<html><body><h1>Hello World</h1></body></html>", title = "helloworld", url = "https://myfreetime.cn"
            };
            var data = new DatabaseEntry();

            data.Data = crawlerRecord.GetBytes();
            //写入操作
            bdb.Put(key, data);
            EMessageBox.Show("写入成功");
        }
        private async void GetHtmlSource(string url, bool isStartWithHttp)
        {
            if (isStartWithHttp == false)
            {
                url = "http://" + url;
            }
            try
            {
                string sourceCode = await WebUtil.GetHtmlSource(url);

                this.rtbox_Resource.Document = new FlowDocument(new Paragraph(new Run(sourceCode)));
                GenerateDOMTree(sourceCode);
            }
            catch (Exception ex)
            {
                EMessageBox.Show(ex.Message);
            }
        }
        private void GetHeaderInfo(string url, bool isStartWithHttp)
        {
            try
            {
                try
                {
                    if (isStartWithHttp == false)
                    {
                        url = "http://" + url;
                    }
                    HttpHeader header = WebUtil.GetHeader(url);


                    Label charsetLabel = new Label();
                    charsetLabel.Margin  = new Thickness(0, 3, 0, 3);
                    charsetLabel.Content = "Charset:" + header.CharSet;

                    //Label contentEncodingLabel = new Label();
                    //contentEncodingLabel.Margin = new Thickness(0, 3, 0, 3);
                    //contentEncodingLabel.Content = "ContentEncoding:" + header.ContentEncoding; //不准确

                    Label contentTypeLabel = new Label();
                    contentTypeLabel.Margin  = new Thickness(0, 3, 0, 3);
                    contentTypeLabel.Content = "ContentType:" + header.ContentType;

                    Label serverNameLabel = new Label();
                    serverNameLabel.Margin  = new Thickness(0, 3, 0, 3);
                    serverNameLabel.Content = "Server:" + header.Server;

                    this.stackpanel.Children.Add(charsetLabel);
                    //this.stackpanel.Children.Add(contentEncodingLabel);
                    this.stackpanel.Children.Add(contentTypeLabel);
                    this.stackpanel.Children.Add(serverNameLabel);
                }
                catch (Exception ex)
                {
                    EMessageBox.Show(ex.Message);
                }
            }
            catch (Exception ex)
            {
                EMessageBox.Show(ex.Message);
            }
        }
        private void GetHostIP(string url, bool isStartWithHttp)
        {
            try
            {
                string ipTempStr = "";

                if (isStartWithHttp)
                {
                    url = url.Replace("http://", "");
                    url = url.Replace("https://", "");
                }

                url = url.Substring(0, url.IndexOf(UrlUtil.UrlPathSeparator));

                WrapPanel wrapPanel   = new WrapPanel();
                Label     hostIPLabel = new Label();
                hostIPLabel.Width  = 80;
                hostIPLabel.Margin = new Thickness(0, 3, 0, 3);
                IPAddress[] hostIPAddresses = WebUtil.GetHostIP(url);
                foreach (var item in hostIPAddresses)
                {
                    ipTempStr += item.ToString() + ";";
                }
                hostIPLabel.Content = "服务器IP:";
                TextBox hostIPTbx = new TextBox();
                hostIPTbx.Width  = 300;
                hostIPTbx.Margin = new Thickness(0, 3, 0, 3);
                hostIPTbx.VerticalContentAlignment = VerticalAlignment.Center;
                hostIPTbx.IsReadOnly = true;
                hostIPTbx.Text       = ipTempStr;
                wrapPanel.Children.Add(hostIPLabel);
                wrapPanel.Children.Add(hostIPTbx);

                //TODO
                //IP查询

                this.stackpanel.Children.Add(wrapPanel);
            }
            catch (Exception ex)
            {
                EMessageBox.Show(ex.Message);
            }
        }
Example #25
0
        private void btn_Fetch_Click(object sender, RoutedEventArgs e)
        {
            string hostName = this.tbox_Url.Text.Trim();

            if (string.IsNullOrEmpty(hostName))
            {
                EMessageBox.Show("请输入主机名,如www.baidu.com");
                return;
            }

            try
            {
                string content = WebUtil.GetHtmlSourceWithSocket(hostName);
                this.rtbox_Content.Document = new FlowDocument(new Paragraph(new Run(content)));
            }
            catch (Exception ex)
            {
                ShowStatusMessage(ex.Message);
            }
        }
        private void btn_WriteImageToBDB_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog fileDialog = new Microsoft.Win32.OpenFileDialog();
            fileDialog.Filter           = "图像文件|*.jpg;*.png;*.bmp;|全部文件|*.*";
            fileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            if (fileDialog.ShowDialog().Value == true)
            {
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.UriSource   = new Uri(fileDialog.FileName);
                bi.CacheOption = BitmapCacheOption.OnLoad;
                bi.EndInit();
                this.img.Source = bi;

                var key = new DatabaseEntry();
                key.Data = Encoding.ASCII.GetBytes("TestImageKey");
                bdb.Put(key, fileDialog.FileName);

                EMessageBox.Show("写入成功");
            }
        }
Example #27
0
 private void Match_Event(object sender, RoutedEventArgs e)
 {
     try
     {
         Button          button  = sender as Button;
         var             grid    = VisualTreeHelper.GetParent(button) as Grid;
         var             pattern = (grid.Children[1] as TextBox).Text;
         var             input   = (grid.Children[3] as TextBox).Text;
         MatchCollection mc      = Regex.Matches(input, pattern);
         var             result  = "";
         for (int i = 0; i < mc.Count; i++)
         {
             result += mc[i].Value + ";";
         }
         (grid.Children[5] as TextBox).Text = result;
     }
     catch (Exception ex)
     {
         EMessageBox.Show(ex.Message);
     }
 }
        private async void GetHtmlSource(string url, bool isStartWithHttp, Encoding encoding = null)
        {
            if (isStartWithHttp == false)
            {
                url = "http://" + url;
            }
            try
            {
                string sourceCode = await WebUtil.GetHtmlSource(url, encoding);

                RichTextBox richTextBox = new RichTextBox();
                richTextBox.Height = this.stackpanel.ActualHeight - 180;
                richTextBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                richTextBox.Document = new FlowDocument(new Paragraph(new Run(sourceCode)));
                this.stackpanel.Children.Add(richTextBox);
            }
            catch (Exception ex)
            {
                EMessageBox.Show(ex.Message);
            }
        }
        private void btn_Fetch_Click(object sender, RoutedEventArgs e)
        {
            string url = this.tbox_Url.Text;

            if (UrlUtil.IsEmpty(url))
            {
                EMessageBox.Show("请输入网址");
                this.tbox_Url.Focus();
                return;
            }

            bool isStartWithHttp = false;

            if (RegexUtil.IsUrl(url, out isStartWithHttp) == false)
            {
                EMessageBox.Show("网址输入错误");
                this.tbox_Url.Focus();
                return;
            }

            GetHtmlSource(url, isStartWithHttp);
        }
        private async void btn_Donwload_Click(object sender, RoutedEventArgs e)
        {
            string url = this.tbox_Url.Text.Trim();

            if (string.IsNullOrEmpty(url))
            {
                EMessageBox.Show("请输入下载地址");
                return;
            }

            if (globalData.CrawlerConfig.ImageConfig.IgnoreUrlCheck == false)
            {
                //TODO
                //文件URL检验
            }

            ShowStatusText($"check {url} status");
            HttpHeader httpHeader = WebUtil.GetHeader(url);

            if (httpHeader.StatusCode != System.Net.HttpStatusCode.OK)
            {
                ShowStatusText($"{url} is not available");
                return;
            }

            ShowStatusText($"{url} is available");
            ShowStatusText($"Download file {url}.....");
            string fileName = await WebUtil.DownloadFileAsync(url);

            ShowStatusText($"Download : {url} Finished\r\n");

            //Temp
            if (System.IO.Path.GetExtension(url) == ".mp4")
            {
                (Application.Current.MainWindow as MainWindow).SetTransparentBackground();
                (Application.Current.MainWindow as MainWindow).SetBackgroundVideo(fileName);
            }
        }