C#網(wǎng)頁信息采集方法匯總
更新時(shí)間:2014年10月29日 09:31:49 投稿:shichen2014
這篇文章主要介紹了C#網(wǎng)頁信息采集方法,實(shí)例匯總了三種常用的方法,是非常實(shí)用的技巧,需要的朋友可以參考下
本文實(shí)例總結(jié)了三種常用的C#網(wǎng)頁信息采集方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
一、通過HttpWebResponse 來獲取
復(fù)制代碼 代碼如下:
public static string CheckTeamSiteUrl(string url)
{
string response = "";
HttpWebResponse httpResponse = null;
//assert: user have access to URL
try
{
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Headers.Set("Pragma", "no-cache");
// request.Headers.Set("KeepAlive", "true");
httpRequest.CookieContainer = new CookieContainer();
httpRequest.Referer = url;
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
httpRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
}
catch (Exception ex)
{
throw new ApplicationException("HTTP 403 Access denied, URL: " + url, ex);
}
//if here, the URL is correct and the user has access
try
{
string strEncod = httpResponse.ContentType;
StreamReader stream;
if (strEncod.ToLower().IndexOf("utf") != -1)
{
stream = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.UTF8);
}
else
{
stream = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.Default);
}
char[] buff = new char[4000];
stream.ReadBlock(buff,0,4000);
response = new string(buff);
stream.Close();
httpResponse.Close();
}
catch (Exception ex)
{
throw new ApplicationException("HTTP 404 Page not found, URL: " + url, ex);
}
return response;
}
{
string response = "";
HttpWebResponse httpResponse = null;
//assert: user have access to URL
try
{
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Headers.Set("Pragma", "no-cache");
// request.Headers.Set("KeepAlive", "true");
httpRequest.CookieContainer = new CookieContainer();
httpRequest.Referer = url;
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
httpRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
}
catch (Exception ex)
{
throw new ApplicationException("HTTP 403 Access denied, URL: " + url, ex);
}
//if here, the URL is correct and the user has access
try
{
string strEncod = httpResponse.ContentType;
StreamReader stream;
if (strEncod.ToLower().IndexOf("utf") != -1)
{
stream = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.UTF8);
}
else
{
stream = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.Default);
}
char[] buff = new char[4000];
stream.ReadBlock(buff,0,4000);
response = new string(buff);
stream.Close();
httpResponse.Close();
}
catch (Exception ex)
{
throw new ApplicationException("HTTP 404 Page not found, URL: " + url, ex);
}
return response;
}
二、通過 WebResponse 來獲取
復(fù)制代碼 代碼如下:
public static string getPage(String url)
{
WebResponse result = null;
string resultstring = "";
try
{
WebRequest req = WebRequest.Create(url);
req.Timeout = 30000;
result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
//read the stream into a string
//StreamReader sr = new StreamReader(ReceiveStream, System.Text.Encoding.UTF8);
string strEncod = result.ContentType;
StreamReader sr;
if (strEncod.ToLower().IndexOf("utf") != -1)
{
sr = new StreamReader(ReceiveStream, System.Text.Encoding.UTF8);
}
else
{
sr = new StreamReader(ReceiveStream, System.Text.Encoding.Default);
}
resultstring = sr.ReadToEnd();
js.alert(resultstring);
//Console.WriteLine(resultstring);
}
catch
{
throw new Exception();
}
finally
{
if (result != null)
{
result.Close();
}
}
return resultstring;
}
{
WebResponse result = null;
string resultstring = "";
try
{
WebRequest req = WebRequest.Create(url);
req.Timeout = 30000;
result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
//read the stream into a string
//StreamReader sr = new StreamReader(ReceiveStream, System.Text.Encoding.UTF8);
string strEncod = result.ContentType;
StreamReader sr;
if (strEncod.ToLower().IndexOf("utf") != -1)
{
sr = new StreamReader(ReceiveStream, System.Text.Encoding.UTF8);
}
else
{
sr = new StreamReader(ReceiveStream, System.Text.Encoding.Default);
}
resultstring = sr.ReadToEnd();
js.alert(resultstring);
//Console.WriteLine(resultstring);
}
catch
{
throw new Exception();
}
finally
{
if (result != null)
{
result.Close();
}
}
return resultstring;
}
三、通過WebClient來獲取
復(fù)制代碼 代碼如下:
public string get(int length)
{
try
{
getEncodeing();
WebClient wb = new WebClient();
Stream response = wb.OpenRead(url);
StreamReader reader = new StreamReader(response, this.encoding, true, 256000);
char[] a = new char[length];
int i = reader.Read(a,0,length);
reader.Close();
return new string(a);
}
catch (Exception e)
{
return e.Message;
//return null;
}
}
private void getEncodeing()
{
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;
}
}
{
try
{
getEncodeing();
WebClient wb = new WebClient();
Stream response = wb.OpenRead(url);
StreamReader reader = new StreamReader(response, this.encoding, true, 256000);
char[] a = new char[length];
int i = reader.Read(a,0,length);
reader.Close();
return new string(a);
}
catch (Exception e)
{
return e.Message;
//return null;
}
}
private void getEncodeing()
{
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;
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- asp.net(c#)做一個(gè)網(wǎng)頁數(shù)據(jù)采集工具
- C#結(jié)合數(shù)據(jù)庫的數(shù)據(jù)采集器示例
- C#實(shí)現(xiàn)的中國移動(dòng)官網(wǎng)手機(jī)號(hào)碼采集器
- Python制作爬蟲采集小說
- Python采集騰訊新聞實(shí)例
- Python爬蟲_城市公交、地鐵站點(diǎn)和線路數(shù)據(jù)采集實(shí)例
- python實(shí)現(xiàn)自動(dòng)登錄人人網(wǎng)并采集信息的方法
- 使用C# CefSharp Python采集某網(wǎng)站簡歷并且自動(dòng)發(fā)送邀請短信的方法
相關(guān)文章
Unity實(shí)現(xiàn)高效的音效管理類的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何通過Unity實(shí)現(xiàn)高效的音效管理類,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下2023-03-03
C#實(shí)現(xiàn)WinForm全屏置頂?shù)氖纠a
我們在運(yùn)行一些?Windows?應(yīng)用程序的時(shí)候,需要將其運(yùn)行在窗體置頂?shù)哪J?并且進(jìn)入全屏狀態(tài),本文將介紹如何使用?C#?來實(shí)現(xiàn)?WinForm?的全屏置頂?shù)幕竟δ?感興趣的可以了解下2024-12-12
C#使用log4net結(jié)合sqlite數(shù)據(jù)庫實(shí)現(xiàn)記錄日志
因?yàn)榻Y(jié)構(gòu)化的數(shù)據(jù)庫存儲(chǔ)的日志信息,可以寫專門的軟件讀取歷史日志信息,通過各種條件篩選,可操作性極大增強(qiáng),有這方面需求的開發(fā)人員可以考慮,本文給大家介紹了C#使用log4net結(jié)合sqlite數(shù)據(jù)庫記錄日志,需要的朋友可以參考下2024-10-10
unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周
這篇文章主要介紹了unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04
C#查詢SqlServer數(shù)據(jù)庫并返回單個(gè)值的方法
這篇文章主要介紹了C#查詢SqlServer數(shù)據(jù)庫并返回單個(gè)值的方法,涉及C#操作SQLServer數(shù)據(jù)庫查詢的相關(guān)技巧,需要的朋友可以參考下2015-06-06

