.net 文件下载上传、网页抓取 用WebClient实现
2010-08-06 15:25
441 查看
.net 文件下载上传、网页抓取 用WebClient实现
我们知道用 WebRequest(HttpWebRequest、FtpWebRequest) 和 WebResponse(HttpWebResponse、FtpWebResponse)可以实现文件下载上传、网页抓取,可是用 WebClient 更轻松。
源:http://www.cftea.com/c/2010/04/N71KIVEHY9BRFER5.asp
我们知道用 WebRequest(HttpWebRequest、FtpWebRequest) 和 WebResponse(HttpWebResponse、FtpWebResponse)可以实现文件下载上传、网页抓取,可是用 WebClient 更轻松。
//用 DownloadFile 下载网页 //就这样,http://www.cftea.com/ 首页就被保存到 C 盘下了。 using (System.Net.WebClient client = new System.Net.WebClient()) { client.DownloadFile("http://www.cftea.com/", "C:\\foo.txt"); } //用 DownloadData 抓取网页 // using (System.Net.WebClient client = new System.Net.WebClient()) { byte[] bytes = client.DownloadData("http://www.cftea.com/"); string str = System.Text.Encoding.GetEncoding("gb2312").GetString(bytes); } //用 OpenRead 抓取网页 //我们将抓取来的网页赋给变量 str,任由我们使用。也可以用 OpenRead 方法来获取数据流。 using (System.Net.WebClient client = new System.Net.WebClient()) { using (System.IO.Stream stream = client.OpenRead("http://www.cftea.com/")) { using (System.IO.StreamReader reader = new System.IO.StreamReader(stream, System.Text.Encoding.GetEncoding("gb2312"))) { string str = reader.ReadToEnd(); reader.Close(); } stream.Close(); } } //用 UploadFile 上传文件 //相对于 DownloadData、OpenRead,WebClient 也具有 UploadData、OpenWrite 方法,但最常用的恐怕还是上传文件,也就是用方法 UploadFile。 //注意 UploadFile 的第一个参数,要把上传后形成的文件名加上去,也就是说这里不能是:ftp://www.cftea.com/。 using (System.Net.WebClient client = new System.Net.WebClient()) { client.Credentials = new System.Net.NetworkCredential("用户名", "密码"); client.UploadFile("ftp://www.cftea.com/foo.txt", "C:\\foo.txt"); }
源:http://www.cftea.com/c/2010/04/N71KIVEHY9BRFER5.asp
相关文章推荐
- .net 文件下载上传、网页抓取 用WebClient实现
- .net 文件下载上传、网页抓取 用WebClient实现
- 用 WebClient 轻松实现文件下载上传、网页抓取
- WebClient 轻松实现文件下载上传、网页抓取
- .NET实现文件跨服务器上传下载的方法
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- .NET中实现文件的上传与下载
- C# WinForm通过WebClient实现文件上传下载 (附源码) 分类: C#.NET
- php使用curl 实现GET和POST请求(抓取网页,上传文件),支持跨项目和跨服务器
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- PHP 利用 Curl Functions 实现多线程抓取网页和下载文件
- silverlight webclient实现上传、下载、删除、读取文件
- Flex与.NET互操作 使用FileReference+HttpHandler实现文件上传/下载
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- 拥有网页版小U盘 ASP.NET实现文件上传与下载功能
- .Net 2.0对文件传输协议(FTP)操作(上传,下载,新建,删除,FTP间传送文件等)实现汇总
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- 使用FileReference+HttpHandler实现文件上传/下载(Flex与.NET互操作)
- C# WinForm通过WebClient实现文件上传下载 (附源码)
- ASP.NET上传下载文件示例,使用webClient实现