|
|
发表于 2016-2-26 16:23:12
|
显示全部楼层
" |' Y8 x; s3 c7 g: B' l' d缺少一个gethtml,用下面这个:7 g% u5 I1 ?0 p; |3 x5 p/ @
- public string getHtml(string url, string charSet, bool UseUTF8CharSet)//url是要访问的网站地址,charSet是目标网页的编码,如果传入的是null或者"",那就自动分析网页的编码 " [1 [! w1 ]! A$ e( Q m+ P+ S
- {
6 X' w) \' Z4 t$ Q# F - string strWebData = "error";9 t$ q( {0 O# u! m3 |
- try6 `: Y& l; G6 K3 N, i e
- {
) p' O! p# z/ P9 n - WebClient myWebClient = new WebClient(); //创建WebClient实例myWebClient # o4 l! z7 i0 V" B
- // 需要注意的:
: A/ \! a8 |+ t2 g: Z$ U( U( Z - //有的网页可能下不下来,有种种原因比如需要cookie,编码问题等等
* p5 W" d$ Q) O: |+ ` - //这是就要具体问题具体分析比如在头部加入cookie
9 y- B, W7 s' C - // webclient.Headers.Add("Cookie", cookie); : `+ n3 O j1 N2 h1 T2 j
- //这样可能需要一些重载方法。根据需要写就可以了. ^0 D+ y' q e" _% u) z. f
- 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)");1 H' x; p& C6 r! A: ^5 ]
- //myWebClient.Headers.Add("User-agent", "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");% U) J% _7 k+ L* L. w
- //获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
9 w) B, O2 T- ?' ~ - myWebClient.Credentials = CredentialCache.DefaultCredentials;) ]) e; d0 R5 T
- //如果服务器要验证用户名,密码 . |$ R6 S7 s: X9 H3 q3 P. z3 ^
- //NetworkCredential mycred = new NetworkCredential(struser, strpassword); , m, h* J& b9 b. r8 o* A
- //myWebClient.Credentials = mycred; 0 H+ R8 }4 w) \
- //从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号) # n7 s1 P/ |* C
- byte[] myDataBuffer = myWebClient.DownloadData(url);. B# S/ \( C6 w& U5 g2 [; }
- strWebData = Encoding.Default.GetString(myDataBuffer);, n8 G% ? F: B! R* A) y
( X7 \7 g, [! p. J- //获取网页字符编码描述信息 ! ^% r" P* }( p0 P# {+ P
- Match charSetMatch = Regex.Match(strWebData, "<meta([^<]*)charset=([^<]*)"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
h6 Q6 ^9 y/ ?& K8 B - string webCharSet = charSetMatch.Groups[2].Value;# Z# Z7 J2 R; @) |, y7 D3 L7 P3 s
- if (charSet == null || charSet == "")
o7 E* z% ], s - charSet = webCharSet;
. @+ K, x( ]( D+ k- [' e# Q - if (charSet.Length > 0)
" E* l: j) K% w t/ \+ Y; C. c - {% y2 f* L; M; J! {' w
- charSet = charSet.Replace(""", "");
3 A1 U4 f* _' t$ e' M$ ~: k5 M - }
% O/ b( F' G! V# \. l8 u, B - if (UseUTF8CharSet)& f+ y' e) g" d* u: n* ~
- {
: f( h! s% K i O1 O! F - if (charSet == null || charSet.Length == 0); r' a5 s9 p' D$ j
- {
+ Q8 Q# G+ c3 ?' G - charSet = "utf-8";
) d' v$ R' ~4 l W9 u - }/ B$ h. {' P, d! a0 l% H1 \, e+ X
- }
/ u) O7 \0 @5 b' W4 [ - if (charSet != null && charSet != "" && Encoding.GetEncoding(charSet) != Encoding.Default): W4 J1 E. c* Q' o
- strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);! r9 s( H+ E5 D% J
- , g# U+ z3 b; ~, `' ]9 l* @
- }4 _" t- N9 A+ a7 A4 ?6 O4 k
- catch (Exception)
2 m5 i+ k/ o, T* ]' B! [ - {
3 b, `0 Y9 S4 a% }: d* N - strWebData = "error";; G' m# b* M, }5 o+ I% Q
- }
3 Z3 @. @% `! k# a! f - 0 G4 N v Z& V$ P w" Y
- return strWebData;
1 k$ w$ [2 x* u - }
复制代码
/ a9 `/ W, V4 q' c% C* i' |; ^; r; [& d) [" V+ B8 t+ m4 F0 \
|
|