ASP.NET 讀取HTML

  • 6112
  • 0

摘要:ASP.NET 讀取HTML

  1. 讀取本頁最終產生的 HTML:覆寫本頁的Render()。通常有此需求都是要對 HTML做調整…
    http://blog.darkthread.net/blogs/darkthreadtw/archive/2008/05/09/replace-html-of-asp-net.aspx 
        protected override void Render(HtmlTextWriter writer)
        {
            //自己做一個假的HtmlTextWriter去把HTML騙回來,草船借箭!!
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.IO.StreamWriter sw = new System.IO.StreamWriter(ms);

            HtmlTextWriter fakeHtmlTextWriter = new HtmlTextWriter(sw);
            base.Render(fakeHtmlTextWriter);

            sw.Flush();

            string html = sw.Encoding.GetString(ms.ToArray());

            //更動內容後,輸出到真正的HtmlTextWriter
            writer.Write(html.Replace("blog.darkthread.net", "blog2.darkthread.net"));
       }


     

  2. 讀取外部網頁的 HTML:
      public string LoadHTML(string URL)
      {
          WebRequest myWebRequest = WebRequest.Create(URL);  
          WebResponse myWebResponse = myWebRequest.GetResponse();  
          Stream myStream = myWebResponse.GetResponseStream();
          Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
          StreamReader myStreamReader = new StreamReader( myStream, encode );
          string html=myStreamReader.ReadToEnd();
          myWebResponse.Close();
          return html;
      }

    然後…有登入驗證的網頁它讀不到XD