zoukankan      html  css  js  c++  java
  • .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;
        }

  • 相关阅读:
    如何解决tensorflow报:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
    windows10下如何进行源码编译安装tensorflow
    windows10如何安装cpu版本tensorflow
    git:RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Reques t Entity Too Larg
    ubuntu 18.04 64bit如何安装GPU版本tensorflow
    pip安装tensorflow-gpu好慢怎么办
    linux下split切割的文件如何合并
    Java使用FileOutputStream写入文件
    springmvc文件上传 参数为MultipartFile 转换为File
    Java中FileOutputStream流的write方法
  • 原文地址:https://www.cnblogs.com/yasin/p/1928978.html
Copyright © 2011-2022 走看看