- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Diagnostics;
- using System.Text.RegularExpressions;
- using System.IO;
- using System.Net;
-
- namespace 采集测试
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] urls= {
- "http://www.yongfa365.com",
- "http://www.cbdcn.com",
- "http://www.csdn.net",
- "http://www.sina.com",
- "http://www.tom.com",
- };
-
- string html="";
-
- html = CaiJi.GetHtmlSource("http://www.yongfa365.com", Encoding.Default);
- Console.Write(html);
- Console.ReadKey();
-
- html = CaiJi.GetHtmlSource("http://www.baidu.com/");
- Console.Write(html);
- Console.ReadKey();
-
- html = CaiJi.GetHtmlSource("http://www.tom.com","utf-8");
- Console.Write(html);
- Console.ReadKey();
-
- foreach (string url in urls)
- {
- Console.Write(CaiJi.GetHtmlSource(url));
- Console.ReadKey();
-
- }
- }
- }
- }
-
-
-
-
-
- class CaiJi
- {
-
-
-
-
-
-
- public static string GetHtmlSource(string url, string charset)
- {
-
- Encoding nowCharset;
- if (charset == "" || charset == null)
- {
- nowCharset = Encoding.Default;
- }
- else
- {
- nowCharset = Encoding.GetEncoding(charset);
- }
-
-
- string html = "";
- try
- {
-
-
-
-
-
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- Stream stream = response.GetResponseStream();
- StreamReader reader = new StreamReader(stream, nowCharset);
- html = reader.ReadToEnd();
- stream.Close();
- }
- catch (Exception e)
- {
- }
- return html;
- }
-
-
-
-
-
-
-
- public static string GetHtmlSource(string url, Encoding charset)
- {
-
- string html = "";
- try
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- Stream stream = response.GetResponseStream();
- StreamReader reader = new StreamReader(stream, charset);
- html = reader.ReadToEnd();
- stream.Close();
- }
- catch (Exception e)
- {
- }
- return html;
- }
-
-
-
-
-
-
-
- public static string GetHtmlSource(string url)
- {
-
- string html = "";
- try
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- Stream stream = response.GetResponseStream();
- StreamReader reader = new StreamReader(stream, Encoding.Default);
- html = reader.ReadToEnd();
- stream.Close();
- }
- catch (Exception e)
- {
- }
- return html;
- }
- }
posted on
2010-01-22 22:54
野人哥哥
阅读(
558)
评论()
编辑
收藏
举报