|
|
发表于 2016-2-26 16:23:12
|
显示全部楼层
% [, ?9 P) F3 u5 k. B
缺少一个gethtml,用下面这个:8 Q/ e$ d! w! U
- public string getHtml(string url, string charSet, bool UseUTF8CharSet)//url是要访问的网站地址,charSet是目标网页的编码,如果传入的是null或者"",那就自动分析网页的编码 5 N2 t d& H: l8 Q1 S/ }; L8 D& \" K
- {
2 V* B4 e! H/ @" P2 _ - string strWebData = "error";# s% y- q7 v: z5 ]; M
- try5 Y% H* f& [( j* m9 o
- {
& ]* S( X, t6 U - WebClient myWebClient = new WebClient(); //创建WebClient实例myWebClient [( P) q' `3 F6 j4 B. K
- // 需要注意的: 4 U4 [8 E* i, B1 M
- //有的网页可能下不下来,有种种原因比如需要cookie,编码问题等等
D* F1 r8 n5 ~; l - //这是就要具体问题具体分析比如在头部加入cookie . m! E. _0 p& @
- // webclient.Headers.Add("Cookie", cookie);
; \, m/ Q5 A S1 `9 G& } - //这样可能需要一些重载方法。根据需要写就可以了
3 |, T2 L+ n6 x6 t; d - 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)");
% X. [; i) T& v3 Z1 `+ d - //myWebClient.Headers.Add("User-agent", "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");
9 Z! c7 |: f2 d$ x( w2 m - //获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
$ q. D9 {; x1 T- ` - myWebClient.Credentials = CredentialCache.DefaultCredentials;
2 `; V+ J; z0 [! S8 l" D - //如果服务器要验证用户名,密码 & l2 s0 P' y7 _% H9 z7 o) ~3 |
- //NetworkCredential mycred = new NetworkCredential(struser, strpassword); / O% ]% g8 h( O2 c+ z2 g" N3 N
- //myWebClient.Credentials = mycred; $ y' {0 M4 M4 p
- //从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号) ; D( N6 s; Z( m
- byte[] myDataBuffer = myWebClient.DownloadData(url);
5 k! A2 R: |* M/ h# e- J - strWebData = Encoding.Default.GetString(myDataBuffer);
& D# H/ @1 `0 o( q* v+ q& O8 z- @
$ O/ ~& G3 Z2 o9 P9 z8 V/ ?$ a- //获取网页字符编码描述信息
; k3 |7 A5 v" l0 a% o5 G! \" b8 D6 g* Z - Match charSetMatch = Regex.Match(strWebData, "<meta([^<]*)charset=([^<]*)"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
: f L! [" K* V; D - string webCharSet = charSetMatch.Groups[2].Value;
+ U" H/ C8 y7 o7 ~7 g7 ~ - if (charSet == null || charSet == "")" Z. N3 J- T( s+ }" R d/ }$ K
- charSet = webCharSet;
* k9 d. u# `( b$ R5 O% C! K+ U$ v - if (charSet.Length > 0)
2 R `% B4 ?1 w - {
! Y; P, T: }) b - charSet = charSet.Replace(""", "");- W4 C! a. B+ \; I
- }7 J! G1 _4 p2 s! q3 L) u
- if (UseUTF8CharSet)
& M6 ^, W6 K$ m4 k - {
' j+ P* g. l/ H/ x1 A - if (charSet == null || charSet.Length == 0)" t) }) f4 N: \$ X
- {7 x( ~4 M, n5 }: \# s
- charSet = "utf-8";
2 g3 D- G& k$ D - }4 a8 e" |5 H8 i( w
- }
9 y9 C3 b4 L y0 \7 d$ V8 c: v - if (charSet != null && charSet != "" && Encoding.GetEncoding(charSet) != Encoding.Default)! K5 Q8 T# A |9 d! P
- strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);( A3 S' W* k% z7 E& b; C3 D# I
: L3 n+ ~0 ]- E) I+ P" n% ]9 b& i- }. J# f8 e+ @% i$ K5 k# k
- catch (Exception)
- C) s* B% I# |$ y& [( U - {$ V" K" E" C @+ f. x
- strWebData = "error";, C" v* j1 i) I1 m
- }/ [- B) `0 g$ I) t. u! e$ y7 l
- % Y5 n& Q/ H: O9 |
- return strWebData;
5 g7 ]6 k! b3 u/ j+ M( ~& }2 x - }
复制代码 0 D! b$ r( T" G
8 i1 c% @9 e9 }# c |
|