C# 读取网页源代码

using System.Net;
using System.IO;

// 方法一
WebRequest request = WebRequest.Create("http://www.baidu.com");
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
Encoding encoding = Encoding.Default;
StreamReader reader = new StreamReader(stream, encoding);
String line="";
while ((line = reader.ReadLine()) != null)
{
Response.Write(line);
}

// 方法二

WebClient webC = new WebClient();
Stream stream=webC.OpenRead("http://www.baidu.com");
Encoding encoding = Encoding.Default;
StreamReader reader = new StreamReader(stream, encoding);
String str = reader.ReadToEnd();

Response.Write(str);

你可能感兴趣的:(源代码)