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


C# StreamReader.ToCharArray方法代码示例

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


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

示例1: Init

    private void Init(string _url)
    {
        try
        {
            m_uri = new Uri(_url);
            m_links = new List<Link>();
            m_html = "";
            m_outstr = "";
            m_title = "";
            m_good = true;
            if (_url.EndsWith(".rar") || _url.EndsWith(".dat") || _url.EndsWith(".msi"))
            {
                m_good = false;
                return;
            }
            HttpWebRequest rqst = (HttpWebRequest)WebRequest.Create(m_uri);
            rqst.AllowAutoRedirect = true;
            rqst.MaximumAutomaticRedirections = 3;
            rqst.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
            rqst.KeepAlive = true;
            rqst.Timeout = 10000;
            lock (WebPage.webcookies)
            {
                if (WebPage.webcookies.ContainsKey(m_uri.Host))
                    rqst.CookieContainer = WebPage.webcookies[m_uri.Host];
                else
                {
                    CookieContainer cc = new CookieContainer();
                    WebPage.webcookies[m_uri.Host] = cc;
                    rqst.CookieContainer = cc;
                }
            }
            HttpWebResponse rsps = (HttpWebResponse)rqst.GetResponse();
            Stream sm = rsps.GetResponseStream();
            if (!rsps.ContentType.ToLower().StartsWith("text/") || rsps.ContentLength > 1 << 22)
            {
                rsps.Close();
                m_good = false;
                return;
            }
            Encoding cding = System.Text.Encoding.Default;
            string contenttype = rsps.ContentType.ToLower();
            int ix = contenttype.IndexOf("charset=");
            if (ix != -1)
            {
                try
                {
                    cding = System.Text.Encoding.GetEncoding(rsps.ContentType.Substring(ix + "charset".Length + 1));
                }
                catch
                {
                    cding = Encoding.Default;
                }

                //该处视情况而定 有的需要解码
                //m_html = HttpUtility.HtmlDecode(new StreamReader(sm, cding).ReadToEnd());
                m_html = new StreamReader(sm, cding).ReadToEnd();

            }
            else
            {
                //该处视情况而定 有的需要解码
                //m_html = HttpUtility.HtmlDecode(new StreamReader(sm, cding).ReadToEnd());

                m_html = new StreamReader(sm, cding).ReadToEnd();
                Regex regex = new Regex("charset=(?<cding>[^=]+)?\"", RegexOptions.IgnoreCase);
                string strcding = regex.Match(m_html).Groups["cding"].Value;
                try
                {
                    cding = Encoding.GetEncoding(strcding);
                }
                catch
                {
                    cding = Encoding.Default;
                }
                byte[] bytes = Encoding.Default.GetBytes(m_html.ToCharArray());
                m_html = cding.GetString(bytes);
                if (m_html.Split('?').Length > 100)
                {
                    m_html = Encoding.Default.GetString(bytes);
                }
            }
            m_pagesize = m_html.Length;
            m_uri = rsps.ResponseUri;
            rsps.Close();
        }
        catch (Exception ex)
        {

        }
    }
开发者ID:priceLiu,项目名称:GetWebPage,代码行数:91,代码来源:WegPage.cs


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