|
|
发表于 2016-2-26 16:23:12
|
显示全部楼层
" E6 V% A) ~/ q1 O8 n- c& G, h% s
缺少一个gethtml,用下面这个:. s) l! ]! M* y* U! v) k
- public string getHtml(string url, string charSet, bool UseUTF8CharSet)//url是要访问的网站地址,charSet是目标网页的编码,如果传入的是null或者"",那就自动分析网页的编码 . o7 y" r1 Z8 W7 g
- {( x% i- X# v: [- o0 q6 M
- string strWebData = "error";5 J( A0 ?1 {7 |; R2 N
- try& [ k6 o) z7 i, Y
- {7 t2 I( `% m4 x }
- WebClient myWebClient = new WebClient(); //创建WebClient实例myWebClient . H0 {+ m2 p6 x" G Y6 p( ]6 S
- // 需要注意的: ' D$ @7 F3 B' |9 U0 C
- //有的网页可能下不下来,有种种原因比如需要cookie,编码问题等等
w; F1 J2 ^- L# Q$ o - //这是就要具体问题具体分析比如在头部加入cookie , P; e9 J' Q/ i4 W# u1 n$ T
- // webclient.Headers.Add("Cookie", cookie);
4 q* v: u+ S: w1 J - //这样可能需要一些重载方法。根据需要写就可以了
5 f" O0 u/ e: i, t5 A N2 p - 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 f7 E1 k, M8 W8 { - //myWebClient.Headers.Add("User-agent", "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");
/ F. i% \; [; y( X: I - //获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
+ c; |: k+ L! @5 R/ G# w - myWebClient.Credentials = CredentialCache.DefaultCredentials;2 ]" G+ n- G6 P3 _; |, p
- //如果服务器要验证用户名,密码 % D" O( g: Q" p! k, ]8 E$ [0 J/ z3 @
- //NetworkCredential mycred = new NetworkCredential(struser, strpassword); " A `3 h$ s) Q% K( y& o* ?
- //myWebClient.Credentials = mycred; 9 P: u3 H* q0 Z. r5 e. _& n
- //从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号) # A$ \ Y/ U% h( p7 `
- byte[] myDataBuffer = myWebClient.DownloadData(url);
% T) z c2 K& T$ i# } - strWebData = Encoding.Default.GetString(myDataBuffer);" W# u' H6 H6 h2 N s; l
- . H6 O( d, v+ K1 n# M/ R
- //获取网页字符编码描述信息
9 _5 W: Y2 s) o s" W. a% w - Match charSetMatch = Regex.Match(strWebData, "<meta([^<]*)charset=([^<]*)"", RegexOptions.IgnoreCase | RegexOptions.Multiline);( v3 \6 `# @: q
- string webCharSet = charSetMatch.Groups[2].Value;
4 d9 C% Z; N/ V# g2 R7 o5 @+ h9 B - if (charSet == null || charSet == "")0 t( ?4 \) |* z3 f+ v1 I8 V! e
- charSet = webCharSet;
( S; p$ _6 Z! p0 M - if (charSet.Length > 0)* n2 K4 [7 J' H- X u& `/ K& Z
- {
# w! |3 F! f% P* I" ^ - charSet = charSet.Replace(""", "");9 {. M) ]- G6 s0 `; A
- }
3 a/ [0 W- ?- b O - if (UseUTF8CharSet)" f: {% ?/ d9 d G2 P9 {
- {3 M2 [: k. V( ~' y0 o
- if (charSet == null || charSet.Length == 0)
! P0 |1 u, d0 _9 Q& U. r. T - {# q6 N4 y( E6 E! }1 v' R
- charSet = "utf-8";
( Q/ ]+ k5 `' \4 r5 I3 p# J - }
/ m m2 U4 Z% O9 y5 ` - }
3 s! `0 M& S) p! r- p, ]; b; n - if (charSet != null && charSet != "" && Encoding.GetEncoding(charSet) != Encoding.Default)
: y5 g6 G; s& Q/ K: C P) m, _1 X5 S$ L - strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);$ n" u3 K) w$ U9 h2 B8 X$ p' l. F
- ) C! F/ b7 p: |5 o: ~5 M. V
- }
! G% y, F8 X N - catch (Exception)
6 o# T3 d/ ]; p: N7 A - {
' T! ^2 z4 t+ e+ B6 [: n3 a9 c, ~ - strWebData = "error";
1 B% d; O$ c: g. L) W' ?: h - }
4 b, ]( X! N" i" ]/ N& Q; |, y9 ` - ' ^- x h: g0 w# I, g
- return strWebData;3 d h& C. N c X) h% j/ ^# m
- }
复制代码 : W2 a% K" d4 F, o; }0 V. K
8 @1 g7 f) a8 L3 F
|
|