{"id":778,"date":"2007-02-28T17:38:58","date_gmt":"2007-02-28T17:38:58","guid":{"rendered":"http:\/\/ellery.no-ip.info\/wp\/thinkingmore\/2007\/02\/28\/%e8%ae%80%e5%8f%96%e7%b6%b2%e9%a0%811\/"},"modified":"2007-02-28T17:38:58","modified_gmt":"2007-02-28T17:38:58","slug":"%e8%ae%80%e5%8f%96%e7%b6%b2%e9%a0%811","status":"publish","type":"post","link":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/2007\/02\/%e8%ae%80%e5%8f%96%e7%b6%b2%e9%a0%811\/","title":{"rendered":"\u8b80\u53d6\u7db2\u9801(1)"},"content":{"rendered":"<p>\u5982\u679c\u4f60\u8981\u641e\u500b spider \u6216\u662f bot\uff0c\u9700\u8981\u8b80\u53d6\u7db2\u9801\u7684\u8a71\uff0c\u7528 WebClient \u5c31\u7dbd\u7dbd\u6709\u9918\u4e86\uff5e<br \/>\n<font color=\"#0000FF\">using<\/font> System.Net;<\/p>\n<p><font color=\"#0000FF\">public<\/font> <font color=\"#0000FF\">class<\/font> Network<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"#0000FF\">public<\/font> <font color=\"#0000FF\">static<\/font> <font color=\"#0000FF\">string<\/font> wget( <font color=\"#0000FF\">string<\/font> url )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"#008080\">WebClient<\/font> _client=<font color=\"#0000FF\">new<\/font> <font color=\"#008080\">WebClient<\/font>();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"#0000FF\">string<\/font> result=<font color=\"#800000\">&quot;&quot;<\/font>;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"#0000FF\">try<\/font> {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"#008000\">\/\/ \u85c9\u8457\u4fee\u6539 Header\uff0c\u53ef\u4ee5\u7528\u4f86\u6a21\u64ec\u67d0\u7279\u5b9a Browser\uff0c\u4ee5\u4e0b\u662f\u6a21\u64ec IE 6<\/font><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_client.Headers.Add(<font color=\"#800000\">&quot;Accept&quot;<\/font>,<font color=\"#800000\">&quot;image\/gif, image\/x-xbitmap, image\/jpeg, image\/pjpeg, *\/*&quot;<\/font>);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_client.Headers.Add(<font color=\"#800000\">&quot;Accept-Language&quot;<\/font>,<font color=\"#800000\">&quot;zh-tw&quot;<\/font>);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_client.Headers.Add(<font color=\"#800000\">&quot;User-Agent&quot;<\/font>,<font color=\"#800000\">&quot;Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)&quot;<\/font>);<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"#008000\">\/\/ \u8868\u793a\u652f\u63f4\u58d3\u7e2e\uff0c\u4e5f\u5c31\u662f\u8aaa\uff0c\u4f60\u53ef\u4ee5\u4e1f\u58d3\u7e2e\u904e\u7684\u8cc7\u6599\u904e\u4f86\uff0c\u6211\u4f86\u89e3\uff5e<\/font><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"#008000\">\/\/_client.Headers.Add(&quot;Accept-Encoding&quot;,&quot;gzip, deflate&quot;);<\/font><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"#008000\">\/\/ read<\/font><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.IO.<font color=\"#008080\">Stream<\/font> objStream=_client.OpenRead( url );<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"#008000\">\/\/ \u8981\u77e5\u9053\u6b63\u78ba\u7684\u7de8\u78bc\uff0c\u518d\u53bb\u8b80\u53d6\uff5e.Net\u6703\u5e6b\u6211\u5011\u81ea\u52d5\u8f49\u70ba unicode \u5b57\u4e32\u3002\u9019\u908a\u9810\u8a2d\u90fd\u662f UTF8<\/font><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.IO.<font color=\"#008080\">StreamReader<\/font> _reader=<font color=\"#0000FF\">new<\/font> System.IO.<font color=\"#008080\">StreamReader<\/font>(objStream,System.Text.<font color=\"#008080\">Encoding<\/font>.UTF8);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"#008000\">\/\/System.IO.StreamReader _reader=new System.IO.StreamReader(objStream,System.Text.Encoding.GetEncoding(950));<\/font><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = _reader.ReadToEnd();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"#0000FF\">catch<\/font>( <font color=\"#008080\">Exception<\/font> ex ) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"#0000FF\">throw<\/font> ex;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"#0000FF\">finally<\/font> {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color=\"#0000FF\">return<\/font> result;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n}<\/p>\n<p>\u6240\u4ee5\u6211\u5011\u5c31\u53ef\u4ee5\u9019\u9ebc\u7528<br \/>\n<font color=\"#008080\">Console<\/font>.WriteLine( Network.wget(<font color=\"#800000\">&quot;http:\/\/www.google.com.tw&quot;<\/font>) );<\/p>\n<p>\u76ee\u524d\u6709\u500b\u6c7a\u5b9a\u6027\u7684\u7f3a\u9ede\uff1a\u5fc5\u9808\u5148\u77e5\u9053\u7db2\u9801\u7de8\u78bc\uff0c\u6211\u5011\u624d\u80fd\u8b80\u5230\u6b63\u78ba\u7684\u6587\u5b57\uff5e\u6709\u8fa6\u6cd5\u5148\u77e5\u9053\u7db2\u9801\u7684\u7de8\u78bc\uff0c\u518d\u53bb\u6c7a\u5b9a\u8981\u600e\u9ebc\u8b80\u53d6\u55ce\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u4f60\u8981\u641e\u500b spider \u6216\u662f bot\uff0c\u9700\u8981\u8b80\u53d6\u7db2\u9801\u7684\u8a71\uff0c\u7528 WebClient \u5c31\u7dbd\u7dbd\u6709\u9918\u4e86\uff5e using System.Net; public class Network { &nbsp;&nbsp;&nbsp;&nbsp;public static string wget( string url ) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebClient _client=new WebClient(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string result=&quot;&quot;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u85c9\u8457\u4fee\u6539 Header\uff0c\u53ef\u4ee5\u7528\u4f86\u6a21\u64ec\u67d0\u7279\u5b9a Browser\uff0c\u4ee5\u4e0b\u662f\u6a21\u64ec IE 6 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_client.Headers.Add(&quot;Accept&quot;,&quot;image\/gif, image\/x-xbitmap, image\/jpeg, image\/pjpeg, *\/*&quot;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_client.Headers.Add(&quot;Accept-Language&quot;,&quot;zh-tw&quot;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_client.Headers.Add(&quot;User-Agent&quot;,&quot;Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)&quot;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/2007\/02\/%e8%ae%80%e5%8f%96%e7%b6%b2%e9%a0%811\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;\u8b80\u53d6\u7db2\u9801(1)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[6],"tags":[],"class_list":["post-778","post","type-post","status-publish","format-standard","hentry","category-net"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2MOxp-cy","_links":{"self":[{"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/posts\/778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/comments?post=778"}],"version-history":[{"count":0,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/posts\/778\/revisions"}],"wp:attachment":[{"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/media?parent=778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/categories?post=778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/tags?post=778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}