C#网页信息采集方法汇总
本文实例总结了三种常用的C#网页信息采集方法。分享给大家供大家参考。具体实现方法如下:
一、通过HttpWebResponse来获取
publicstaticstringCheckTeamSiteUrl(stringurl) { stringresponse=""; HttpWebResponsehttpResponse=null; //assert:userhaveaccesstoURL try { HttpWebRequesthttpRequest=(HttpWebRequest)WebRequest.Create(url); httpRequest.Headers.Set("Pragma","no-cache"); //request.Headers.Set("KeepAlive","true"); httpRequest.CookieContainer=newCookieContainer(); httpRequest.Referer=url; httpRequest.UserAgent="Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.0;.NETCLR1.1.4322;.NETCLR2.0.50727)"; httpRequest.Credentials=System.Net.CredentialCache.DefaultCredentials; httpResponse=(HttpWebResponse)httpRequest.GetResponse(); } catch(Exceptionex) { thrownewApplicationException("HTTP403Accessdenied,URL:"+url,ex); } //ifhere,theURLiscorrectandtheuserhasaccess try { stringstrEncod=httpResponse.ContentType; StreamReaderstream; if(strEncod.ToLower().IndexOf("utf")!=-1) { stream=newStreamReader(httpResponse.GetResponseStream(),System.Text.Encoding.UTF8); } else { stream=newStreamReader(httpResponse.GetResponseStream(),System.Text.Encoding.Default); } char[]buff=newchar[4000]; stream.ReadBlock(buff,0,4000); response=newstring(buff); stream.Close(); httpResponse.Close(); } catch(Exceptionex) { thrownewApplicationException("HTTP404Pagenotfound,URL:"+url,ex); } returnresponse; }
二、通过WebResponse来获取
publicstaticstringgetPage(Stringurl) { WebResponseresult=null; stringresultstring=""; try { WebRequestreq=WebRequest.Create(url); req.Timeout=30000; result=req.GetResponse(); StreamReceiveStream=result.GetResponseStream(); //readthestreamintoastring //StreamReadersr=newStreamReader(ReceiveStream,System.Text.Encoding.UTF8); stringstrEncod=result.ContentType; StreamReadersr; if(strEncod.ToLower().IndexOf("utf")!=-1) { sr=newStreamReader(ReceiveStream,System.Text.Encoding.UTF8); } else { sr=newStreamReader(ReceiveStream,System.Text.Encoding.Default); } resultstring=sr.ReadToEnd(); js.alert(resultstring); //Console.WriteLine(resultstring); } catch { thrownewException(); } finally { if(result!=null) { result.Close(); } } returnresultstring; }
三、通过WebClient来获取
publicstringget(intlength) { try { getEncodeing(); WebClientwb=newWebClient(); Streamresponse=wb.OpenRead(url); StreamReaderreader=newStreamReader(response,this.encoding,true,256000); char[]a=newchar[length]; inti =reader.Read(a,0,length); reader.Close(); returnnewstring(a); } catch(Exceptione) { returne.Message; //returnnull; } } privatevoidgetEncodeing() { switch(this.encode) { case"UTF-8":encoding=Encoding.UTF8;break; case"GB2312":encoding=Encoding.GetEncoding("GB2312");break; case"ASCII":encoding=Encoding.ASCII;break; default:encoding=Encoding.GetEncoding(encode);break; } }
希望本文所述对大家的C#程序设计有所帮助。