C# download 静态网页

主要内容如题,实现了三个完成download静态网页的方法。

分别是用TcpClient建立连接;用WebRequest和WebResponse下载,然后是更简单的Webclient。

以下是源码,包含较详细注释。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.IO;
namespace DNSIP
{
    class Program
    {
        static void Main(string[] args)
        {
            string Url = "http://www.baidu.com";
            //TcpClient client = new TcpClient();
            //client.Connect(Url, 80);
            //Console.WriteLine("done");
            //client.Close();
            //string DownFile = getHtml1(Url);
           // Console.WriteLine(DownFile);


            //string Downfile = getHtml2(Url);
            string Downfile = getHtml3(Url);


            ////write this file into a txt
            //String filename = "conyent.html";
            //FileInfo t = new FileInfo(filename);
            //StreamWriter tex = t.CreateText();
            //tex.WriteLine(Downfile);
            //tex.Close();
            //Console.WriteLine("Done, Thanks!");


            Console.WriteLine(Downfile);
            Console.ReadKey();
            
        }

        ///Download whole html Page --Use TcpClient
        public static string getHtml1(string URLName)
        {
            string page ="";
            TcpClient client = new TcpClient();
            int portNumber = 80;

            try
            {
                client.Connect(URLName, portNumber);
                Console.WriteLine("Connected!");

                NetworkStream clientStream = client.GetStream();
                StreamWriter writerStream = new StreamWriter(clientStream);
                writerStream.Write("GET/HTTP/1.1\r\n" + "User-Agent:crawler request!\r\n" + "Host:www.baidu.com\r\n" + "Connection:Close\r\n" + "\r\n");
                writerStream.Flush();

                string text = "";
                byte[] buffer = new byte[1024];
                while (clientStream.Read(buffer, 0, 1024) > 0)
                {
                    text = text + Encoding.UTF8.GetString(buffer);
                }
                page = text;
            }
            catch (SocketException ex)
            {
                System.Console.WriteLine(ex.Message);
            }
            finally {
                client.Close();
            }
            return page;
        }///done

        ///Download whole html page --Use WebRequest
        public static string getHtml2(string URLName)
        {
            HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(URLName);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Encoding encode = Encoding.GetEncoding("utf-8");
            TextReader tr = new StreamReader(response.GetResponseStream(), encode);
            String HtmlContent = tr.ReadToEnd();

            response.Close();
            return HtmlContent;
          }///done
        
        ///Download whole file page --use Webclient
        public static string getHtml3(string URLName)
        {
            WebClient webclient = new WebClient();
            Stream stream = webclient.OpenRead(URLName);
            StreamReader reader = new StreamReader(stream);
            string strResult = reader.ReadToEnd();
            reader.Close();
            stream.Close();
            webclient.Dispose();
            return strResult;
        }///done

    }
}
上面得到的结果一是直接在Console中write的,另一个是写在了一个content.html文件中的。下面是结果截图。

C# download 静态网页_第1张图片

C# download 静态网页_第2张图片

你可能感兴趣的:(C#,下载,静态网页)