• .NET下获取网页源码的几种方法


    /// <summary>
        /// 使用WebClient方法
        /// </summary>
        protected void WebClientButton_Click(object sender, EventArgs e)
        {
            pageUrl = UrlText.Text;
            WebClient wc = new WebClient();
            wc.Credentials = CredentialCache.DefaultCredentials;

            //方法一
            byte[] pageData = wc.DownloadData(pageUrl);
            ContentHtml.Text = Encoding.Default.GetString(pageData);

            /*
            //方法二
            Stream resStream = wc.OpenRead(pageurl);
            StreamReader sr = new StreamReader(resStream, Encoding.Default);
            ContentHtml.Text = sr.ReadToEnd();
            resStream.Close();
            */

            wc.Dispose();
        }

        /// <summary>
        /// 使用WebRequest方法
        /// </summary>
        protected void WebRequestButton_Click(object sender, EventArgs e)
        {
            pageUrl = UrlText.Text;
            WebRequest wrequest = WebRequest.Create(pageUrl);
            WebResponse wresponse = wrequest.GetResponse();
            Stream resStream = wresponse.GetResponseStream();
            StreamReader sr = new StreamReader(resStream, Encoding.Default);
            ContentHtml.Text = sr.ReadToEnd();
            resStream.Close();
            sr.Close();
        }

    public string GetRemoteHtmlCode(string Url)
        {
            string s = null;
            try
            {
                MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
                _xmlhttp.open("GET", Url, false, null, null);
                _xmlhttp.send("");
                if (_xmlhttp.readyState == 4)
                {
                    s = System.Text.Encoding.UTF8.GetString((byte[])_xmlhttp.responseBody);
                }

               
            }
            catch
            {
            }
            return s;
        }

        public string GetRemoteHtmlCodeByGB2312(string Url)
        {
            string s = null;
            try
            {
                MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
                _xmlhttp.open("GET", Url, false, null, null);
                _xmlhttp.send("");
                if (_xmlhttp.readyState == 4)
                {
                    s = System.Text.Encoding.Default.GetString((byte[])_xmlhttp.responseBody);
                }
            }
            catch
            {
            }
            return s;
        }

  • 相关阅读:
    专家视角 | 小荷的 Oracle Database 18c 新特性快速一瞥
    java.lang.ClassCastException: com.xx.User cannot be cast to com.xx.User
    上传单个文件
    极速体验:Oracle 18c 下载和Scalable Sequence新特性
    开工大吉:Oracle 18c已经发布及新特性介绍
    CentOS7编译安装NodeJS
    .NET 同步与异步之锁(ReaderWriterLockSlim)(八)
    .NET 同步与异步之锁(ReaderWriterLockSlim)(八)
    .NET 同步与异步之锁(ReaderWriterLockSlim)(八)
    .NET 同步与异步之锁(ReaderWriterLockSlim)(八)
  • 原文地址:https://www.cnblogs.com/yasin/p/1928978.html
Copyright © 2020-2023  润新知