1 |
<ol class="dp-c"><li class="alt"><span><span>1.通过HttpWebResponse 来获取 </span></span></li><li><span><span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">string</span><span> CheckTeamSiteUrl(</span><span class="keyword">string</span><span> url) </span></span></li><li class="alt"><span> { </span></li><li><span> <span class="keyword">string</span><span> response = </span><span class="string">""</span><span>; </span></span></li><li class="alt"><span> HttpWebResponse httpResponse = <span class="keyword">null</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span> <span class="comment">//assert: user have access to URL </span><span> </span></span></li><li><span> <span class="keyword">try</span><span> </span></span></li><li class="alt"><span> { </span></li><li><span> HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url); </span></li><li class="alt"><span> httpRequest.Headers.Set(<span class="string">"Pragma"</span><span>, </span><span class="string">"no-cache"</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span> <span class="comment">// request.Headers.Set("KeepAlive", "true"); </span><span> </span></span></li><li><span> </span></li><li class="alt"><span> httpRequest.CookieContainer = <span class="keyword">new</span><span> CookieContainer(); </span></span></li><li><span> </span></li><li class="alt"><span> </span></li><li><span> </span></li><li class="alt"><span> httpRequest.Referer = url; </span></li><li><span> </span></li><li class="alt"><span> httpRequest.UserAgent = <span class="string">"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span> </span></li><li><span> </span></li><li class="alt"><span> httpRequest.Credentials = System.Net.CredentialCache.DefaultCredentials; </span></li><li><span> httpResponse = (HttpWebResponse)httpRequest.GetResponse(); </span></li><li class="alt"><span> </span></li><li><span> } </span></li><li class="alt"><span> <span class="keyword">catch</span><span> (Exception ex) </span></span></li><li><span> { </span></li><li class="alt"><span> <span class="keyword">throw</span><span> </span><span class="keyword">new</span><span> ApplicationException(</span><span class="string">"HTTP 403 Access denied, URL: "</span><span> + url, ex); </span></span></li><li><span> } </span></li><li class="alt"><span> </span></li><li><span> <span class="comment">//if here, the URL is correct and the user has access </span><span> </span></span></li><li class="alt"><span> <span class="keyword">try</span><span> </span></span></li><li><span> { </span></li><li class="alt"><span> <span class="keyword">string</span><span> strEncod = httpResponse.ContentType; </span></span></li><li><span> StreamReader stream; </span></li><li class="alt"><span> <span class="keyword">if</span><span> (strEncod.ToLower().IndexOf(</span><span class="string">"utf"</span><span>) != -1) </span></span></li><li><span> { </span></li><li class="alt"><span> stream = <span class="keyword">new</span><span> StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.UTF8); </span></span></li><li><span> } </span></li><li class="alt"><span> <span class="keyword">else</span><span> </span></span></li><li><span> { </span></li><li class="alt"><span> stream = <span class="keyword">new</span><span> StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.Default); </span></span></li><li><span> } </span></li><li class="alt"><span> </span></li><li><span> <span class="keyword">char</span><span>[] buff = </span><span class="keyword">new</span><span> </span><span class="keyword">char</span><span>[4000]; </span></span></li><li class="alt"><span> stream.ReadBlock(buff,0,4000); </span></li><li><span> response = <span class="keyword">new</span><span> </span><span class="keyword">string</span><span>(buff); </span></span></li><li class="alt"><span> stream.Close(); </span></li><li><span> httpResponse.Close(); </span></li><li class="alt"><span> } </span></li><li><span> <span class="keyword">catch</span><span> (Exception ex) </span></span></li><li class="alt"><span> { </span></li><li><span> <span class="keyword">throw</span><span> </span><span class="keyword">new</span><span> ApplicationException(</span><span class="string">"HTTP 404 Page not found, URL: "</span><span> + url, ex); </span></span></li><li class="alt"><span> } </span></li><li><span> <span class="keyword">return</span><span> response; </span></span></li><li class="alt"><span> </span></li><li><span> } </span></li><li class="alt"><span> </span></li><li> </li><li><span>2.通过 WebResponse 来获取 </span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">string</span><span> getPage(String url) </span></span></li><li><span> { </span></li><li class="alt"><span> WebResponse result = <span class="keyword">null</span><span>; </span></span></li><li><span> <span class="keyword">string</span><span> resultstring = </span><span class="string">""</span><span>; </span></span></li><li class="alt"><span> <span class="keyword">try</span><span> </span></span></li><li><span> { </span></li><li class="alt"><span> WebRequest req = WebRequest.Create(url); </span></li><li><span> req.Timeout = 30000; </span></li><li class="alt"><span> result = req.GetResponse(); </span></li><li><span> Stream ReceiveStream = result.GetResponseStream(); </span></li><li class="alt"><span> </span></li><li><span> <span class="comment">//read the stream into a string </span><span> </span></span></li><li class="alt"><span> <span class="comment">//StreamReader sr = new StreamReader(ReceiveStream, System.Text.Encoding.UTF8); </span><span> </span></span></li><li><span> <span class="keyword">string</span><span> strEncod = result.ContentType; </span></span></li><li class="alt"><span> StreamReader sr; </span></li><li><span> <span class="keyword">if</span><span> (strEncod.ToLower().IndexOf(</span><span class="string">"utf"</span><span>) != -1) </span></span></li><li class="alt"><span> { </span></li><li><span> sr = <span class="keyword">new</span><span> StreamReader(ReceiveStream, System.Text.Encoding.UTF8); </span></span></li><li class="alt"><span> } </span></li><li><span> <span class="keyword">else</span><span> </span></span></li><li class="alt"><span> { </span></li><li><span> sr = <span class="keyword">new</span><span> StreamReader(ReceiveStream, System.Text.Encoding.Default); </span></span></li><li class="alt"><span> } </span></li><li><span> resultstring = sr.ReadToEnd(); </span></li><li class="alt"><span> js.alert(resultstring); </span></li><li><span> <span class="comment">//Console.WriteLine(resultstring); </span><span> </span></span></li><li class="alt"><span> } </span></li><li><span> <span class="keyword">catch</span><span> </span></span></li><li class="alt"><span> { </span></li><li><span> <span class="keyword">throw</span><span> </span><span class="keyword">new</span><span> Exception(); </span></span></li><li class="alt"><span> } </span></li><li><span> <span class="keyword">finally</span><span> </span></span></li><li class="alt"><span> { </span></li><li><span> <span class="keyword">if</span><span> (result != </span><span class="keyword">null</span><span>) </span></span></li><li class="alt"><span> { </span></li><li><span> result.Close(); </span></li><li class="alt"><span> } </span></li><li><span> } </span></li><li class="alt"><span> <span class="keyword">return</span><span> resultstring; </span></span></li><li><span> } </span></li><li class="alt"><span> </span></li><li> </li><li><span>3.通过WebClient来获取 </span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">string</span><span> </span><span class="keyword">get</span><span>(</span><span class="keyword">int</span><span> length) </span></span></li><li><span> { </span></li><li class="alt"><span> <span class="keyword">try</span><span> </span></span></li><li><span> { </span></li><li class="alt"><span> getEncodeing(); </span></li><li><span> WebClient wb = <span class="keyword">new</span><span> WebClient(); </span></span></li><li class="alt"><span> Stream response = wb.OpenRead(url); </span></li><li><span> StreamReader reader = <span class="keyword">new</span><span> StreamReader(response, </span><span class="keyword">this</span><span>.encoding, </span><span class="keyword">true</span><span>, 256000); </span></span></li><li class="alt"><span> <span class="keyword">char</span><span>[] a = </span><span class="keyword">new</span><span> </span><span class="keyword">char</span><span>[length]; </span></span></li><li><span> <span class="keyword">int</span><span> i = reader.Read(a,0,length); </span></span></li><li class="alt"><span> reader.Close(); </span></li><li><span> <span class="keyword">return</span><span> </span><span class="keyword">new</span><span> </span><span class="keyword">string</span><span>(a); </span></span></li><li class="alt"><span> } </span></li><li><span> <span class="keyword">catch</span><span> (Exception e) </span></span></li><li class="alt"><span> { </span></li><li><span> <span class="keyword">return</span><span> e.Message; </span></span></li><li class="alt"><span> <span class="comment">//return null; </span><span> </span></span></li><li><span> } </span></li><li class="alt"><span> } </span></li><li><span> <span class="keyword">private</span><span> </span><span class="keyword">void</span><span> getEncodeing() </span></span></li><li class="alt"><span> { </span></li><li><span> <span class="keyword">switch</span><span> (</span><span class="keyword">this</span><span>.encode) </span></span></li><li class="alt"><span> { </span></li><li><span> <span class="keyword">case</span><span> </span><span class="string">"UTF-8"</span><span>: encoding = Encoding.UTF8; </span><span class="keyword">break</span><span>; </span></span></li><li class="alt"><span> <span class="keyword">case</span><span> </span><span class="string">"GB2312"</span><span>: encoding = Encoding.GetEncoding(</span><span class="string">"GB2312"</span><span>); </span><span class="keyword">break</span><span>; </span></span></li><li><span> <span class="keyword">case</span><span> </span><span class="string">"ASCII"</span><span>: encoding = Encoding.ASCII; </span><span class="keyword">break</span><span>; </span></span></li><li class="alt"><span> <span class="keyword">default</span><span>: encoding = Encoding.GetEncoding(encode); </span><span class="keyword">break</span><span>; </span></span></li><li><span> } </span></li><li class="alt"><span> } </span></li></ol> |