zoukankan      html  css  js  c++  java
  • 【原创】 ASP.NET C# 远程获取WEB页源代码

    try
            {
                //方法一
                HttpWebRequest webrq = WebRequest.Create("http://www.hiyours.com.cn/index.aspx") as HttpWebRequest;
                HttpWebResponse webrs = webrq.GetResponse() as HttpWebResponse;
                StreamReader sr = new StreamReader(webrs.GetResponseStream(), Encoding.GetEncoding("gb2312"));
                Localize1.Text = Server.HtmlEncode(Regex.Match(sr.ReadToEnd(), "<body[\\s\\S]*?</body>", RegexOptions.IgnoreCase).Value);

                //方法二
                using (WebClient client = new WebClient())
                {
                    client.Headers.Set("User-Agent", "Microsoft Internet Explorer");//此句是关键
                    Byte[] pageData = client.DownloadData("http://www.hiyours.com.cn/index.aspx");
                    Localize1.Text = Server.HtmlEncode(Encoding.GetEncoding("gb2312").GetString(pageData));
                }

                //方法三 只能用于静态读取字符串
                using (StreamReader sr = new StreamReader(Server.MapPath("d.aspx"), Encoding.GetEncoding("gb2312"))) //模板页路径
                {
                    Localize1.Text = Server.HtmlEncode(sr.ReadToEnd());
                    sr.Close();
                }
            }
            catch
            {
                Response.Write("<Script>alert('读取文件错误')</Script>");
            }

  • 相关阅读:
    python基础知识
    Django之模板Template
    Django之视图Views
    Django之model
    kafka保证数据不丢失机制
    kafka和flume进行整合的日志采集的confi文件编写
    kafka-manager监控工具的安装和使用
    CentOS 7 ETCD集群配置
    详细的Docker入门
    ZooKeeper基本原理
  • 原文地址:https://www.cnblogs.com/cosiray/p/1559536.html
Copyright © 2011-2022 走看看