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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 今天早上乘車返回豐順,路上看了一部電影,車上放的,戰(zhàn)狼,這部電影其中有一個情節(jié),令我動容,就是吳京的戰(zhàn)友拿出他閨女...
- 你,是幾個群的群主?很多人會是不止一個。 但,有幾個群你覺得很活躍,甚至從群里變現(xiàn)了?很多人都是0,甚至是負(fù)(投入...