转载自 :http://blog.csdn.net/gisfarmer/article/details/2836904
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.IO;
- namespace thief
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- try {
- WebClient MyWebClient = new WebClient();
- MyWebClient.Credentials = CredentialCache.DefaultCredentials;
- Byte[] pageData = MyWebClient.DownloadData(http:
- string pageHtml = Encoding.Default.GetString(pageData);
-
- Console.WriteLine(pageHtml);
- using (StreamWriter sw = new StreamWriter("c://test//ouput.html"))
- {
- sw.Write(pageHtml);
- }
- Console.ReadLine();
- }
- catch(WebException webEx) {
- Console.WriteLine(webEx.Message.ToString());
- }
- }
- }
- }
改进一下,加入定时器后
- using System;
- using System.Text;
- using System.Timers;
- using System.Net;
- using System.IO;
- namespace TimerTest
- {
- class Program
- {
- public static string outFileName = "";
- public static string myUrl = "http://bxg.cfchina.cn"; //要抓取的网页
- static void Main(string[] args)
- {
- Timer mytimer = new Timer();
- mytimer.Elapsed +=new ElapsedEventHandler(GetUrl);
- mytimer.Interval = 5000;
- mytimer.Start();
- mytimer.Enabled = true;
- while (Console.Read() != ‘q‘)
- {
- }
- }
-
- static void GetUrl(object source, ElapsedEventArgs e)
- {
- try
- {
- WebClient MyWebClient = new WebClient();
- MyWebClient.Credentials = CredentialCache.DefaultCredentials;
- Byte[] pageData = MyWebClient.DownloadData(myUrl);
- string pageHtml = Encoding.Default.GetString(pageData);
-
-
- outFileName = "C://test//" + DateTime.Now.ToString().Replace(" ", "").Replace(":", "").Replace("-", "") + ".html";
- using (StreamWriter sw = new StreamWriter(outFileName))
- {
- sw.Write(pageHtml);
- }
- Console.WriteLine(outFileName);
- }
- catch (WebException webEx)
- {
- Console.WriteLine(webEx.Message.ToString());
- }
- }
- }
- }
C#远程获取/读取网页内容,布布扣,bubuko.com
C#远程获取/读取网页内容
原文:http://www.cnblogs.com/xuxin-1989/p/3896579.html