namespace TagTools
{
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
public class HttpHelper
{
private static Stream _reader;
private static StreamReader _responseReader;
private static HttpWebResponse _webResponse;
public static void Dispose()
{
if (_reader != null)
{
_reader.Close();
}
if (_responseReader != null)
{
_responseReader.Close();
}
if (_webResponse != null)
{
_webResponse.Close();
}
_reader = null;
_responseReader = null;
_webResponse = null;
}
public static string DoHttp(string httpStr)
{
string str;
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(httpStr);
request.Method = "GET";
request.ServicePoint.Expect100Continue = false;
request.Timeout = 0x7530;
using (_webResponse = (HttpWebResponse) request.GetResponse())
{
_reader = _webResponse.GetResponseStream();
if (_reader != null)
{
_responseReader = new StreamReader(_reader, Encoding.UTF8);
str = _responseReader.ReadToEnd();
}
else
{
str = string.Empty;
}
}
if (!string.IsNullOrEmpty(str))
{
return str;
}
return "返回新聞為空";
}
public static string[] GetHtmlImageUrlList(string sHtmlText)
{
MatchCollection matchs = new Regex("<img\\b[^<>]*?\\bsrc[\\s\\t\\r\\n]*=[\\s\\t\\r\\n]*[\"']?[\\s\\t\\r\\n]*(?<imgUrl>[^\\s\\t\\r\\n\"'<>]*)[^<>]*?/?[\\s\\t\\r\\n]*>", RegexOptions.IgnoreCase).Matches(sHtmlText);
int num = 0;
string[] strArray = new string[matchs.Count];
foreach (Match match in matchs)
{
strArray[num++] = match.Groups["imgUrl"].Value;
}
return strArray;
}
public static string GetHtmlTxt(string htmlstring)
{
htmlstring = Regex.Replace(htmlstring, "<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
htmlstring = Regex.Replace(htmlstring, "<(.[^>]*)>", "", RegexOptions.IgnoreCase);
htmlstring = Regex.Replace(htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
htmlstring = Regex.Replace(htmlstring, "-->", "", RegexOptions.IgnoreCase);
htmlstring = Regex.Replace(htmlstring, "<!--.*", "", RegexOptions.IgnoreCase);
htmlstring = Regex.Replace(htmlstring, "&(quot|#34);", "\"", RegexOptions.IgnoreCase);
htmlstring = Regex.Replace(htmlstring, "&(amp|#38);", "&", RegexOptions.IgnoreCase);
htmlstring = Regex.Replace(htmlstring, "&(lt|#60);", "<", RegexOptions.IgnoreCase);
htmlstring = Regex.Replace(htmlstring, "&(gt|#62);", ">", RegexOptions.IgnoreCase);
htmlstring = Regex.Replace(htmlstring, "&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
htmlstring = Regex.Replace(htmlstring, "&(iexcl|#161);", "\x00a1", RegexOptions.IgnoreCase);
htmlstring = Regex.Replace(htmlstring, "&(cent|#162);", "\x00a2", RegexOptions.IgnoreCase);
htmlstring = Regex.Replace(htmlstring, "&(pound|#163);", "\x00a3", RegexOptions.IgnoreCase);
htmlstring = Regex.Replace(htmlstring, "&(copy|#169);", "\x00a9", RegexOptions.IgnoreCase);
htmlstring = Regex.Replace(htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
htmlstring = htmlstring.Replace("<", "");
htmlstring = htmlstring.Replace(">", "");
htmlstring = htmlstring.Replace("\r\n", "");
htmlstring = htmlstring.Replace("ss=\"wzzw\"", "");
return htmlstring;
}
}
}
2019-03-19
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門凿宾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來矾屯,“玉大人,你說我怎么就攤上這事初厚〖希” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵产禾,是天一觀的道長排作。 經(jīng)常有香客問我,道長下愈,這世上最難降的妖魔是什么纽绍? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮势似,結(jié)果婚禮上拌夏,老公的妹妹穿的比我還像新娘僧著。我一直安慰自己,他們只是感情好障簿,可當(dāng)我...
- 文/花漫 我一把揭開白布盹愚。 她就那樣靜靜地躺著,像睡著了一般站故。 火紅的嫁衣襯著肌膚如雪皆怕。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼橱乱,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了粱甫?” 一聲冷哼從身側(cè)響起泳叠,我...
- 正文 年R本政府宣布拙泽,位于F島的核電站淌山,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏顾瞻。R本人自食惡果不足惜泼疑,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望荷荤。 院中可真熱鬧退渗,春花似錦移稳、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至翻翩,卻和暖如春都许,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背嫂冻。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 今天早上乘車返回豐順,路上看了一部電影从铲,車上放的瘪校,戰(zhàn)狼,這部電影其中有一個情節(jié)名段,令我動容阱扬,就是吳京的戰(zhàn)友拿出他閨女...
- 你,是幾個群的群主振湾?很多人會是不止一個杀迹。 但,有幾個群你覺得很活躍押搪,甚至從群里變現(xiàn)了树酪?很多人都是0浅碾,甚至是負(fù)(投入...