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;
        }

  • 相关阅读:
    数论 欧几里德算法 以及 欧几里得拓展
    数论 快速幂的原理讲解
    汉诺塔模板
    C++ 迭代器运算
    C++ STL vector set map 简易用法
    C++ 使用指向函数的指针数组
    Codeforces 718C 线段树+矩乘
    BZOJ 2506 分块
    Codeforces 455D 分块+链表
    Codeforces 19E 树上差分
  • 原文地址:https://www.cnblogs.com/yasin/p/1928978.html
Copyright © 2011-2022 走看看