C# 读取网页源码的三种办法WebClient、WebRequest、HttpWebRequest
来源:互联网 发布:nginx 外网访问内网 编辑:程序博客网 时间:2024/05/22 06:12
直接看这三种办法的源码吧,
using System;using System.IO;using System.Net;namespace ReadHtml{class ReadHtml{static void Main(){System.Console.WriteLine("准备更新数据");string url = "http://www.jiuwola.com/";//string html = new ReadHtml().GetHtmlByWebClient(url);//string html = new ReadHtml().GetHtmlByWebRequest(url);string html = new ReadHtml().GetHtmlByHttpWebRequest(url);System.Console.WriteLine(html);}//第一种private string GetHtmlByWebClient(string url){string html = "";WebClient wc = new WebClient();Stream s = wc.OpenRead(url);StreamReader sr = new StreamReader(s,System.Text.Encoding.GetEncoding("utf-8"));html = sr.ReadToEnd();s.Close();return html;}//第二种private string GetHtmlByWebRequest(string url){Uri uri = new Uri(url);WebRequest request = WebRequest.Create(uri);WebResponse response = request.GetResponse();Stream s = response.GetResponseStream();StreamReader sr = new StreamReader(s,System.Text.Encoding.GetEncoding("utf-8"));string html = sr.ReadToEnd();s.Close();response.Close();return html;}//第三种 建议使用这一种办法private string GetHtmlByHttpWebRequest(string url){Uri uri = new Uri(url);HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);request.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705";request.Accept = "*/*";HttpWebResponse response = (HttpWebResponse)request.GetResponse();Stream s = response.GetResponseStream();StreamReader sr = new StreamReader(s,System.Text.Encoding.GetEncoding("utf-8"));string html = sr.ReadToEnd();s.Close();response.Close();return html;}}}
1 0
- C# 读取网页源码的三种办法WebClient、WebRequest、HttpWebRequest
- c#利用WebClient和WebRequest获取网页源代码的比较
- c#利用WebClient和WebRequest获取网页源代码的比较
- c#利用WebClient和WebRequest获取网页源代码的比较
- c#利用WebClient和WebRequest获取网页源代码的比较
- 在webForm中WebRequest/WebClient/WebBrowser获取远程页面源码的三种方式(downmoon)
- webrequest、httpwebrequest、webclient、HttpClient 四个类的区别?
- C#网页采集数据的几种方式(WebClient、WebBrowser和HttpWebRequest/HttpWebResponse)
- C#网页采集数据的几种方式(WebClient、WebBrowser和HttpWebRequest/HttpWebResponse)
- 【转载】C#网页采集数据的几种方式(WebClient、WebBrowser和HttpWebRequest/HttpWebResponse)
- c#利用WebClient和WebRequest的比较
- 利用WebClient和WebRequest类获得网页源代码C#
- 利用WebClient和WebRequest类获得网页源代码C#
- 利用WebClient和WebRequest类获得网页源代码C#
- c#利用WebClient和WebRequest获取网页源代码
- 利用WebClient和WebRequest类获得网页源代码C#
- C#利用webclient和webrequest来访问网页(已测webclient)
- C# WebRequest WebClient Post请求
- JAVA 几种多线程的简单实例 Thread Runnable
- JAVA HttpURLConnection 获取网页内容
- highcharts 绘制图标的JAVASCRIPT 类库 收藏
- C# 入门之 Hello World
- Linux内核中的GPIO系统之(3):pin controller driver代码分析
- C# 读取网页源码的三种办法WebClient、WebRequest、HttpWebRequest
- 更新系统或者更换Eclipse后,双击Eclipse-drawable,或者在布局文件中,查看图片时,显示乱码。png 和 jpg
- 简单的WINFORM窗体,体验WINFORM带来的快感
- 带有按钮并且可以执行单击事件的WINFORM窗体,体悟C#的创建过程
- iOS面试题 2016版
- C# 打开指定的文件夹 记住路径中 / 与 \ 的用法
- C# 使用 DirectoryInfo 递归指定目录中的所有目录及文件
- C# 用 * 输出两个等腰三角形组成的菱形
- C# RGB颜色转换为16进制