文本框1,输入地址,文本框2显示源码。点击按钮开始获取。关键注意编码,我写了两个,有些网站是utf-8编码,有些是默认,如果一个不行就换另外一个
private void button1_Click(object sender, EventArgs e)
{
WebRequest request = WebRequest.Create(textBox1.Text);//实例化WebRequest对象
WebResponse response = request.GetResponse();//创建WebResponse对象
Stream datastream = response.GetResponseStream();//创建流对象
Encoding ec = Encoding.UTF8;
Encoding EC = Encoding.Default;
StreamReader reader = new StreamReader(datastream, EC);
string responseFromServer = reader.ReadToEnd();//读取数据
richTextBox1.AppendText(responseFromServer);//添加到RichTextBox控件中
reader.Close();
datastream.Close();
response.Close();
}