Example #1
0
        private void ImportFromWebMenu_Click(object sender, EventArgs args)
        {
            Uri url = null;

            var input = _lastKnownGoodImportedUrl != null
                      ? _lastKnownGoodImportedUrl.ToString()
                      : string.Empty;

            do
            {
                input = Interaction.InputBox("Enter URL:", "Import From Web", input,
                                             (int)(Location.X + (Size.Height / 10f)),
                                             (int)(Location.Y + (Size.Height / 10f))).Trim();

                if (string.IsNullOrEmpty(input))
                {
                    return;
                }

                //
                // If some entered just the DNS name to get the home page
                // then we prepend "http://" for the user to prevent typing.
                //
                // http://www.shauninman.com/archive/2006/05/08/validating_domain_names
                //

                if (Regex.IsMatch(input, @"^([a-z0-9] ([-a-z0-9]*[a-z0-9])? \.)+ 
                                            ( (a[cdefgilmnoqrstuwxz]|aero|arpa)
                                              |(b[abdefghijmnorstvwyz]|biz)
                                              |(c[acdfghiklmnorsuvxyz]|cat|com|coop)
                                              |d[ejkmoz]
                                              |(e[ceghrstu]|edu)
                                              |f[ijkmor]
                                              |(g[abdefghilmnpqrstuwy]|gov)
                                              |h[kmnrtu]
                                              |(i[delmnoqrst]|info|int)
                                              |(j[emop]|jobs)
                                              |k[eghimnprwyz]
                                              |l[abcikrstuvy]
                                              |(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)
                                              |(n[acefgilopruz]|name|net)
                                              |(om|org)
                                              |(p[aefghklmnrstwy]|pro)
                                              |qa
                                              |r[eouw]
                                              |s[abcdeghijklmnortvyz]
                                              |(t[cdfghjklmnoprtvwz]|travel)
                                              |u[agkmsyz]
                                              |v[aceginu]
                                              |w[fs]
                                              |y[etu]
                                              |z[amw])", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture))
                {
                    input = "http://" + input;
                }

                if (!Uri.IsWellFormedUriString(input, UriKind.Absolute))
                {
                    MessageBox.Show(this, "The entered URL does not appear to be correctly formatted.", "Invalid URL", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    url = new Uri(input, UriKind.Absolute);
                }
            }while (url == null);

            //
            // Download
            //

            string content;
            var    wc = new WebClient();
            bool   proxyAuthAttempted = false;

            while (true)
            {
                try
                {
                    using (CurrentCursorScope.EnterWait())
                        content = wc.DownloadStringUsingResponseEncoding(url);
                    break;
                }
                catch (WebException e)
                {
                    if (e.Status == WebExceptionStatus.ProtocolError &&
                        !proxyAuthAttempted)
                    {
                        var httpResponse = e.Response as HttpWebResponse;
                        if (httpResponse != null &&
                            HttpStatusCode.ProxyAuthenticationRequired == httpResponse.StatusCode)
                        {
                            proxyAuthAttempted = true;
                            wc.AddHttpWebRequestModifier(hwr => hwr.Proxy.Credentials = CredentialCache.DefaultCredentials);
                            continue;
                        }
                    }

                    Program.ShowExceptionDialog(e, "Import Error", this);
                    return;
                }
            }

            //
            // Make sure it's HTML otherwise get confirmation to proceed.
            //

            var typeHeader  = wc.ResponseHeaders[HttpResponseHeader.ContentType];
            var contentType = new ContentType(typeHeader);

            if (string.IsNullOrEmpty(typeHeader) || !contentType.IsHtml())
            {
                var msg = string.Format(
                    "The downloaded resource is \u201c{0}\u201d rather than HTML, "
                    + "which could produce undesired results. Proceed anyway?",
                    contentType.MediaType);

                if (DialogResult.Yes != MessageBox.Show(this, msg, "Non-HTML Content", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation))
                {
                    return;
                }
            }

            //
            // If it's too big (> 512KB), get confirmation to proceed.
            //

            var  lengthHeader = wc.ResponseHeaders[HttpResponseHeader.ContentLength];
            long size;

            if (long.TryParse(lengthHeader, NumberStyles.None, CultureInfo.InvariantCulture, out size) &&
                size > 512 * 1024)
            {
                var msg = string.Format(
                    "The downloaded resource is rather large ({0} bytes). Proceed anyway?",
                    size.ToString("N0"));

                if (DialogResult.Yes != MessageBox.Show(this, msg, "Large Content", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation))
                {
                    return;
                }
            }

            //
            // Load 'er up!
            //

            using (CurrentCursorScope.EnterWait())
            {
                var document = new HtmlDocument();
                document.LoadHtml2(content);
                Open(document, content);
            }

            _lastKnownGoodImportedUrl = url;
        }
Example #2
0
        private void ImportFromWebMenu_Click(object sender, EventArgs args)
        {
            Uri url = null;

            var input = _lastKnownGoodImportedUrl != null 
                      ? _lastKnownGoodImportedUrl.ToString() 
                      : string.Empty;

            do
            {
                input = Interaction.InputBox("Enter URL:", "Import From Web", input,
                    (int)(Location.X + (Size.Height / 10f)),
                    (int)(Location.Y + (Size.Height / 10f))).Trim();

                if (string.IsNullOrEmpty(input))
                    return;

                //
                // If some entered just the DNS name to get the home page 
                // then we prepend "http://" for the user to prevent typing.
                //
                // http://www.shauninman.com/archive/2006/05/08/validating_domain_names
                //

                if (Regex.IsMatch(input, @"^([a-z0-9] ([-a-z0-9]*[a-z0-9])? \.)+ 
                                            ( (a[cdefgilmnoqrstuwxz]|aero|arpa)
                                              |(b[abdefghijmnorstvwyz]|biz)
                                              |(c[acdfghiklmnorsuvxyz]|cat|com|coop)
                                              |d[ejkmoz]
                                              |(e[ceghrstu]|edu)
                                              |f[ijkmor]
                                              |(g[abdefghilmnpqrstuwy]|gov)
                                              |h[kmnrtu]
                                              |(i[delmnoqrst]|info|int)
                                              |(j[emop]|jobs)
                                              |k[eghimnprwyz]
                                              |l[abcikrstuvy]
                                              |(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)
                                              |(n[acefgilopruz]|name|net)
                                              |(om|org)
                                              |(p[aefghklmnrstwy]|pro)
                                              |qa
                                              |r[eouw]
                                              |s[abcdeghijklmnortvyz]
                                              |(t[cdfghjklmnoprtvwz]|travel)
                                              |u[agkmsyz]
                                              |v[aceginu]
                                              |w[fs]
                                              |y[etu]
                                              |z[amw])", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture))
                {
                    input = "http://" + input;
                }

                if (!Uri.IsWellFormedUriString(input, UriKind.Absolute))
                    MessageBox.Show(this, "The entered URL does not appear to be correctly formatted.", "Invalid URL", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                else
                    url = new Uri(input, UriKind.Absolute);
            }
            while (url == null);

            //
            // Download
            //

            string content;
            var wc = new WebClient();
            bool proxyAuthAttempted = false;

            while (true)
            {
                try
                {
                    using (CurrentCursorScope.EnterWait())
                        content = wc.DownloadStringUsingResponseEncoding(url);
                    break;
                }
                catch (WebException e)
                {
                    if (e.Status == WebExceptionStatus.ProtocolError 
                        && !proxyAuthAttempted)
                    {
                        var httpResponse = e.Response as HttpWebResponse;
                        if (httpResponse != null
                            && HttpStatusCode.ProxyAuthenticationRequired == httpResponse.StatusCode)
                        {
                            proxyAuthAttempted = true;
                            wc.AddHttpWebRequestModifier(hwr => hwr.Proxy.Credentials = CredentialCache.DefaultCredentials);
                            continue;
                        }
                    }

                    Program.ShowExceptionDialog(e, "Import Error", this);
                    return;
                }
            }

            //
            // Make sure it's HTML otherwise get confirmation to proceed.
            //

            var typeHeader = wc.ResponseHeaders[HttpResponseHeader.ContentType];
            var contentType = new ContentType(typeHeader);
            if (string.IsNullOrEmpty(typeHeader) || !contentType.IsHtml())
            {
                var msg = string.Format(
                    "The downloaded resource is \u201c{0}\u201d rather than HTML, "
                    + "which could produce undesired results. Proceed anyway?",
                    contentType.MediaType);

                if (DialogResult.Yes != MessageBox.Show(this, msg, "Non-HTML Content", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation))
                    return;
            }

            //
            // If it's too big (> 512KB), get confirmation to proceed.
            //

            var lengthHeader = wc.ResponseHeaders[HttpResponseHeader.ContentLength];
            long size;
            if (long.TryParse(lengthHeader, NumberStyles.None, CultureInfo.InvariantCulture, out size)
                && size > 512 * 1024)
            {
                var msg = string.Format(
                    "The downloaded resource is rather large ({0} bytes). Proceed anyway?",
                    size.ToString("N0"));

                if (DialogResult.Yes != MessageBox.Show(this, msg, "Large Content", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation))
                    return;
            }

            //
            // Load 'er up!
            //

            using (CurrentCursorScope.EnterWait())
            {
                var document = new HtmlDocument();
                document.LoadHtml2(content);
                Open(document);
            }

            _lastKnownGoodImportedUrl = url;
        }