|
|
发表于 2016-2-26 16:23:12
|
显示全部楼层
) ^0 a; h' I% X: Q
缺少一个gethtml,用下面这个:% U- Z5 N' N, i6 I
- public string getHtml(string url, string charSet, bool UseUTF8CharSet)//url是要访问的网站地址,charSet是目标网页的编码,如果传入的是null或者"",那就自动分析网页的编码
7 ^. v7 L% G6 ~, N - {$ X4 B( ^8 M1 ~, T4 `: \7 i) U) c$ e
- string strWebData = "error";* w. P. f/ u0 G8 X( Z4 G5 s) ~0 Y/ S
- try
' ]7 {" a! _ ^: a; V) t, x - {! @" N/ Y! I# j2 ], S* ^3 H' h0 w
- WebClient myWebClient = new WebClient(); //创建WebClient实例myWebClient
' B8 [9 t/ L. g, k7 x - // 需要注意的:
3 t/ X% D( E# u3 }& k4 i - //有的网页可能下不下来,有种种原因比如需要cookie,编码问题等等 * e- W# |: N: P" f' K
- //这是就要具体问题具体分析比如在头部加入cookie ; a% a1 d7 {% p2 x% O
- // webclient.Headers.Add("Cookie", cookie); ; {% b" K( S. n1 t! |7 _
- //这样可能需要一些重载方法。根据需要写就可以了
, ~: s7 S9 ^* t$ @) a - myWebClient.Headers.Add("User-agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)");
5 h% g" G$ ` ^% U3 K - //myWebClient.Headers.Add("User-agent", "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");
& @+ C, s2 {7 {9 y" e - //获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。 4 F; ~( @* ^ A: H& Z: D1 e1 E
- myWebClient.Credentials = CredentialCache.DefaultCredentials;; Y' N8 y! h! y/ [
- //如果服务器要验证用户名,密码
$ J# t+ A8 E0 d+ \1 c# Z - //NetworkCredential mycred = new NetworkCredential(struser, strpassword); ! m4 @/ M" m* T$ N5 b
- //myWebClient.Credentials = mycred;
7 l: K; f3 S' i - //从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号) . `6 e9 `' H# ?; ]( i" L
- byte[] myDataBuffer = myWebClient.DownloadData(url);' l1 U& E. |# r. [' G+ }
- strWebData = Encoding.Default.GetString(myDataBuffer);( \& }3 O. Y; D/ d
0 _0 I6 q" F8 W* m) V) I- //获取网页字符编码描述信息
; h. M: F& e* z7 ?5 N# x) O+ w0 O - Match charSetMatch = Regex.Match(strWebData, "<meta([^<]*)charset=([^<]*)"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
$ ]8 R$ e( [: X' m - string webCharSet = charSetMatch.Groups[2].Value;
: O" h% U( [0 U4 x' }( @( } - if (charSet == null || charSet == "")- I" @" P+ y/ V& u6 B5 T9 ~+ i
- charSet = webCharSet;
& @) A8 l/ K( j, g) K - if (charSet.Length > 0)
! a1 V% I0 ^, l2 i3 K - {
+ }7 L9 M" C3 p' d: C - charSet = charSet.Replace(""", "");: b, J5 b: T! A! C, j; Z( M- c5 Z
- }
5 P8 j4 U7 D2 d/ `/ O4 M: b$ U - if (UseUTF8CharSet)# n/ {6 E/ \8 o4 @9 q% q) n* D" o
- {
( \! L. E2 l: S, q) v - if (charSet == null || charSet.Length == 0)/ G2 {# a; z- }- u& \- @
- {# w9 B5 O( J) G3 o
- charSet = "utf-8";. w$ @( L3 I2 J$ x
- }
* y: o5 K5 G& _; {* k - }
9 ~+ f6 `6 ?8 Q/ U$ x- w - if (charSet != null && charSet != "" && Encoding.GetEncoding(charSet) != Encoding.Default)! w8 z) s* e3 @5 B5 g
- strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);
' K( g0 O `9 T7 s |
; V- W b$ r, H7 _ @ F- }
; A! L! a, E) V, k' F( G) u3 j - catch (Exception)
H2 O0 y1 P; A5 N - {- `+ S' `1 N o/ r& ^# p' _
- strWebData = "error";0 ~7 e- X8 j% v. G* Y% {6 O/ R
- }8 k: z* \$ i0 W* Z3 v& N7 l3 D
- 9 ~1 D( I& w7 _
- return strWebData;
! }% p7 R; `$ {2 L2 j% U' O) f - }
复制代码 1 z6 [: c' I0 E1 L/ W; ?; _2 w
$ a; Q- P7 l- R: E# v
|
|