当前位置: 首页>>代码示例>>C#>>正文


C# ContentType.IsHtml方法代码示例

本文整理汇总了C#中System.Net.Mime.ContentType.IsHtml方法的典型用法代码示例。如果您正苦于以下问题:C# ContentType.IsHtml方法的具体用法?C# ContentType.IsHtml怎么用?C# ContentType.IsHtml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Mime.ContentType的用法示例。


在下文中一共展示了ContentType.IsHtml方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ImportFromWebMenu_Click


//.........这里部分代码省略.........
                                              |(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;
        }
开发者ID:Sword-Breaker,项目名称:fizzler,代码行数:101,代码来源:MainForm.cs


注:本文中的System.Net.Mime.ContentType.IsHtml方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。