通过C#程序来获取访问页面的内容(网页源代码)并实现将内容保存到本机的文件中。
方法一是通过System.Net的两个关键的类System.Net.HttpWebRequest;System.Net.HttpWebResponse来实现的
具体代码如下:
1 HttpWebRequest httpReq;
2 HttpWebResponse httpResp;
3
4 string strBuff = "";
5 char[] cbuffer = new char[256];
6 int byteRead = 0;
7
8 string filename = @"c:\log.txt";
9 ///定义写入流操作
10 public void WriteStream()
11 {
12 Uri httpURL = new Uri(txtURL.Text);
2 HttpWebResponse httpResp;
3
4 string strBuff = "";
5 char[] cbuffer = new char[256];
6 int byteRead = 0;
7
8 string filename = @"c:\log.txt";
9 ///定义写入流操作
10 public void WriteStream()
11 {
12 Uri httpURL = new Uri(txtURL.Text);
///HttpWebRequest类继承于WebRequest,并没有自己的构造函数,需通过WebRequest的Creat方法 建立,并进行强制的类型转换
13 httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
14 ///通过HttpWebRequest的GetResponse()方法建立HttpWebResponse,强制类型转换
13 httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
14 ///通过HttpWebRequest的GetResponse()方法建立HttpWebResponse,强制类型转换
httpResp = (HttpWebResponse) httpReq.GetResponse();
15 ///GetResponseStream()方法获取HTTP响应的数据流,并尝试取得URL中所指定的网页内容
15 ///GetResponseStream()方法获取HTTP响应的数据流,并尝试取得URL中所指定的网页内容
///若成功取得网页的内容,则以System.IO.Stream形式返回,若失败则产生ProtoclViolationException错 误。在此正确的做法应将以下的代码放到一个try块中处理。这里简单处理
16 Stream respStream = httpResp.GetResponseStream();
16 Stream respStream = httpResp.GetResponseStream();
///返回的内容是Stream形式的,所以可以利用StreamReader类获取GetResponseStream的内容,并以
StreamReader类的Read方法依次读取网页源程序代码每一行的内容,直至行尾(读取的编码格式:UTF8)
17 StreamReader respStreamReader = new StreamReader(respStream,Encoding.UTF8);
18
19 byteRead = respStreamReader.Read(cbuffer,0,256);
20
21 while (byteRead != 0)
22 {
23 string strResp = new string(cbuffer,0,byteRead);
24 strBuff = strBuff + strResp;
25 byteRead = respStreamReader.Read(cbuffer,0,256);
26 }
27
28 respStream.Close();
29 txtHTML.Text = strBuff;
30 }
31 ///定义写入文件操作
32 public void WriteFile()
33 {
17 StreamReader respStreamReader = new StreamReader(respStream,Encoding.UTF8);
18
19 byteRead = respStreamReader.Read(cbuffer,0,256);
20
21 while (byteRead != 0)
22 {
23 string strResp = new string(cbuffer,0,byteRead);
24 strBuff = strBuff + strResp;
25 byteRead = respStreamReader.Read(cbuffer,0,256);
26 }
27
28 respStream.Close();
29 txtHTML.Text = strBuff;
30 }
31 ///定义写入文件操作
32 public void WriteFile()
33 {
///要将结果存储至文件中,可利用FileStream类建立文件数据流,并以Write方法将HTML内容写入到文件
34 FileStream fileStream = new FileStream(filename,FileMode.OpenOrCreate,FileAccess.Write);
35 byte[] byteSave = Encoding.UTF8.GetBytes(txtHTML.Text.ToString()); ///保存为txt文件时编 码方式为UTF8
36
37 fileStream.Write(byteSave,0,byteSave.Length);
38 fileStream.Close();
39 }
40
41 private void btnwrite_Click(object sender, EventArgs e)
42 {
43 WriteStream();
44 WriteFile();
45 }
34 FileStream fileStream = new FileStream(filename,FileMode.OpenOrCreate,FileAccess.Write);
35 byte[] byteSave = Encoding.UTF8.GetBytes(txtHTML.Text.ToString()); ///保存为txt文件时编 码方式为UTF8
36
37 fileStream.Write(byteSave,0,byteSave.Length);
38 fileStream.Close();
39 }
40
41 private void btnwrite_Click(object sender, EventArgs e)
42 {
43 WriteStream();
44 WriteFile();
45 }