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;
}
}
}
网友评论