C# download 静态网页
来源:互联网 发布:手机版数据库软件 编辑:程序博客网 时间:2024/06/05 01:52
主要内容如题,实现了三个完成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文件中的。下面是结果截图。
1 0
- C# download 静态网页
- download file in C#
- 用Java代码download网页
- 静态网页
- 静态网页
- 静态网页
- C# download yahoo stock quote
- C# download file from webserver
- Internet Download Manager IDM 如何抓取网页 整个网页内容
- download
- download
- download
- download
- Download
- download
- download
- download
- download
- 第38章 用访问控制列表实现包过滤
- chrome插件开发-----右键使用百度搜索
- 不是技术牛人,如何拿到国内IT巨头的Offer
- Binder机制(2)
- Cocos2d-x 中使用多线程
- C# download 静态网页
- 我的2014-杂七杂八经历
- openfire用户以及群组关系移植(与数据库中的关联)
- JS - 特殊变量
- Linux端口状态查看、启用和关闭
- 设计模式——单件模式
- java环境变量设置
- 已知某个文件内包含一些电话号码,每个号码为8位数字,统计不同号码的个数。
- ZOJ 1148 The Game (BFS)