C#網(wǎng)頁信息采集方法匯總
更新時間: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;
}
}
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
詳解Unity使用ParticleSystem粒子系統(tǒng)模擬藥水在血管中流動(粒子碰撞)
這篇文章主要介紹了Unity使用ParticleSystem粒子系統(tǒng)模擬藥水在血管中流動(粒子碰撞),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05常用.NET工具(包括.NET可再發(fā)行包2.0)下載
常用.NET工具(包括.NET可再發(fā)行包2.0)下載...2007-03-03C#實(shí)現(xiàn)優(yōu)先隊(duì)列和堆排序
本文詳細(xì)講解了C#實(shí)現(xiàn)優(yōu)先隊(duì)列和堆排序的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04解決C#獲取鼠標(biāo)相對當(dāng)前窗口坐標(biāo)的實(shí)現(xiàn)方法
本篇文章是對在C#中獲取鼠標(biāo)相對當(dāng)前窗口坐標(biāo)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05