Reprinted from: 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; //Gets or sets the network credentials that are used to authenticate requests to Internet resources.
- byte[] pagedata = Mywebclient.downloaddata (http://www.163.com);//download data from the specified Web site
- string pagehtml = Encoding.Default.GetString (pagedata); //If you are using GB2312 to obtain a website page, use this sentence
- //string pagehtml = Encoding.UTF8.GetString (pagedata);//If the Get Site page uses UTF-8, use this sentence
- Console.WriteLine (pagehtml); //Enter what you get in the console
- using (StreamWriter SW = new StreamWriter ("c://test//ouput.html"))//writes the acquired content to the text
- {
- Sw. Write (pagehtml);
- }
- Console.ReadLine (); //Let the console pause, or flash past
- }
- catch (WebException webEx) {
- Console.WriteLine (WebEx.Message.ToString ());
- }
- }
- }
- }
Improve, after adding the timer
- Using System;
- Using System.Text;
- Using System.Timers;
- Using System.Net;
- Using System.IO;
- <summary>
- Fetches the contents of the specified webpage every 5 seconds and saves it as a file in the C:/test directory
- </summary>
- Namespace Timertest
- {
- Class Program
- {
- public static string outfilename = ""; //generated file name
- public static string myurl = "http://bxg.cfchina.cn"; The page to crawl
- static void Main (string[] args)
- {
- Timer MyTimer = new timer ();
- MyTimer. Elapsed + =new Elapsedeventhandler (GETURL); Events that specify Timers
- MyTimer. Interval = 5000; //Catch once every 5 seconds
- MyTimer. Start ();
- MyTimer. Enabled = true;
- while (Console.read ()! = ' q ') //until you press the lowercase letter Q to exit, otherwise crawl down
- {
- }
- }
- //Timer event content
- static void GetUrl (object source, Elapsedeventargs e)
- {
- Try
- {
- WebClient mywebclient = new WebClient ();
- Mywebclient.credentials = CredentialCache.DefaultCredentials; //Gets or sets the network credentials that are used to authenticate requests to Internet resources.
- byte[] Pagedata = Mywebclient.downloaddata (Myurl); //Download data from a specified website
- string pagehtml = Encoding.Default.GetString (pagedata); //If you are using GB2312 to obtain a website page, use this sentence
- //string pagehtml = Encoding.UTF8.GetString (pagedata);//If the Get Site page uses UTF-8, use this sentence
- //console.writeline (pagehtml);//Enter what you get in the console
- Outfilename = "c://test//" + DateTime.Now.ToString (). Replace (" ", ""). Replace (":", ""). Replace ("-", "") + ". html";
- using (StreamWriter SW = new StreamWriter (outfilename))//write the acquired content to the text
- {
- Sw. Write (pagehtml);
- }
- Console.WriteLine (Outfilename); //Output the file name after saving
- }
- catch (WebException webEx)
- {
- Console.WriteLine (WebEx.Message.ToString ());
- }
- }
- }
- }