C#HttpHelper實(shí)現(xiàn)了C#HttpWebRequest抓取時(shí)無視編碼,無視證書,無視Cookie,并且實(shí)現(xiàn)的代理的功能,使用它您可以進(jìn)行Get和Post請(qǐng)求,可以很方便 的設(shè)置Cookie,證書,代理,編碼問題您不用管,因?yàn)轭悤?huì)自動(dòng)為您識(shí)別網(wǎng)頁的編碼。
這個(gè)類是我以前寫百度,Google,Soso,Sogou等網(wǎng)絡(luò)蜘蛛時(shí)使用的,經(jīng)過上千萬個(gè)網(wǎng)站的測(cè)試,上萬個(gè)網(wǎng)站抓取的例子總結(jié)出來的,中間的方法也是我實(shí)驗(yàn)了很久之后方案,所以大家可以放心使用。我不敢說100%,但是應(yīng)該是99%的網(wǎng)站都沒有問題,都可以無視編碼,證書,和Cookie,如果你確實(shí)發(fā)現(xiàn)那個(gè)網(wǎng)站在使用本類過程中有問題,出現(xiàn)亂碼,或者是獲取不了,不能帶Cookie,不能帶證書等問題.
更新日志:
1.4正式版 更新
更新了很多高端問題,優(yōu)化的請(qǐng)求方式,是一個(gè)新技術(shù)跨越的版本
1.3正式版更新
本次主要是修復(fù)了一些測(cè)試版本中的問題,大家可以放心使用
1.3測(cè)試版更新
本來對(duì)類的結(jié)構(gòu)進(jìn)行的重大修改增加了返回參數(shù),并添加了相應(yīng)的Cookie幫助解析類,大家去看看吧
1.2正式版 更新
與上個(gè)版本相比較只是增加了Header參數(shù),可以直接輸入一些不能用屬性設(shè)置的請(qǐng)求參數(shù),并對(duì)代碼結(jié)構(gòu)進(jìn)行的整改,是比較穩(wěn)定的一個(gè)版本。還加入了如何獲取圖片的方法
1.2測(cè)試版
本版本對(duì)Httphelper類的返回參數(shù)進(jìn)行的修改,加入了HttpResult返回參數(shù)類。并對(duì)代理以及性能方面做了重要修改,比如(返回Byte類型,可傳入Byte類型,文件類型等等)
1.1正式版
本版本是最早的一版,只實(shí)現(xiàn)的解決編碼,證書等問題。不過也能滿足大部分人的使用了
在webBrowser中取Cookie的方法
在很多情況下我們會(huì)使用間進(jìn)程的webBrowser去實(shí)現(xiàn)一些網(wǎng)頁的請(qǐng)求和抓去,這個(gè)時(shí)候有部分網(wǎng)頁是取不到Cookie的,那怎么辦呢?下面我提供一個(gè)方法,應(yīng)該99%的都能取到,
//取當(dāng)前webBrowser登錄后的Cookie值
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref int pcchCookieData, int dwFlags, object lpReserved);
//取出Cookie,當(dāng)?shù)卿浐蟛拍苋?
private static string GetCookieString(string url)
{
// Determine the size of the cookie
int datasize = 256;
StringBuilder cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))
{
if (datasize < 0)
return null;
// Allocate stringbuilder large enough to hold the cookie
cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))
return null;
}
return cookieData.ToString();
}
去掉所有的Html代碼
普通瀏覽復(fù)制代碼
/// <summary>
/// 過濾html標(biāo)簽
/// </summary>
/// <param name="strHtml">html的內(nèi)容</param>
/// <returns></returns>
public static string StripHTML(string stringToStrip)
{
// paring using RegEx //
stringToStrip = Regex.Replace(stringToStrip, "</p(?:\\s*)>(?:\\s*)<p(?:\\s*)>", "\n\n", RegexOptions.IgnoreCase | RegexOptions.Compiled);
stringToStrip = Regex.Replace(stringToStrip, "
", "\n", RegexOptions.IgnoreCase | RegexOptions.Compiled);
stringToStrip = Regex.Replace(stringToStrip, "\"", "''", RegexOptions.IgnoreCase | RegexOptions.Compiled);
stringToStrip = StripHtmlXmlTags(stringToStrip);
return stringToStrip;
}
private static string StripHtmlXmlTags(string content)
{
return Regex.Replace(content, "<[^>]+>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
方法如上面所示直接調(diào)用StripHTML方法就行了
4.設(shè)置URl格式的問題
普通瀏覽復(fù)制代碼
public static string URLDecode(string text)
{
return HttpUtility.UrlDecode(text, Encoding.Default);
}
public static string URLEncode(string text)
{
return HttpUtility.UrlEncode(text, Encoding.Default);
}
方法也是直接調(diào)用就行了一個(gè)加密一個(gè)解密大家可以直接使用