2019-03-19

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;
        }
    }
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 漫漫 -----致R 床头开始漫延 属于衣服的沙沙的细语 像是那晚我们的说出口的 和没有说出口的 ...
    半余阅读 230评论 0 0
  • 今天早上乘车返回丰顺,路上看了一部电影,车上放的,战狼,这部电影其中有一个情节,令我动容,就是吴京的战友拿出他闺女...
    麻麻爸爸阅读 433评论 1 2
  • 不念过往, 不畏将来。 是个状态。 做得到继续, 做不到倒地。
    lapetite33明天在哪里阅读 228评论 0 0
  • 你,是几个群的群主?很多人会是不止一个。 但,有几个群你觉得很活跃,甚至从群里变现了?很多人都是0,甚至是负(投入...
    哎卖姜阅读 6,855评论 2 8
  • 日复一日,我们寻求亚里士多德在《伦理学》中提出的那一古老问题的答案:一个人应该如何度过他的一生?但,问题的答案总在...
    画重点阅读 766评论 0 1

友情链接更多精彩内容