using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Text.RegularExpressions; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string url = textBox1.Text.Trim(); //需要获取源代码的网页 //建立连接 WebRequest request = WebRequest.Create(url); //WebRequest.Create方法,返回WebRequest的子类HttpWebRequest //获取相应 WebResponse response = request.GetResponse(); //WebRequest.GetResponse方法,返回对 Internet 请求的响应 响应相当于缓存 Stream resStream = response.GetResponseStream(); //WebResponse.GetResponseStream 方法,从 Internet 资源返回数据流。 Encoding enc = Encoding.GetEncoding("utf-8"); // 如果是乱码就改成 utf-8 / GB2312 StreamReader sr = new StreamReader(resStream, enc); //命名空间:System.IO。 StreamReader 类实现一个 TextReader (TextReader类,表示可读取连续字符系列的读取器),使其以一种特定的编码从字节流中读取字符。 ContentHtml.Text = sr.ReadToEnd(); //输出(HTML代码),ContentHtml为Multiline模式的TextBox控件 } } }
时间: 2024-10-15 10:31:24