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

?著作權(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ù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 漫漫 -----致R 床頭開始漫延 屬于衣服的沙沙的細(xì)語 像是那晚我們的說出口的 和沒有說出口的 ...
    半余閱讀 227評論 0 0
  • 今天早上乘車返回豐順,路上看了一部電影,車上放的,戰(zhàn)狼,這部電影其中有一個情節(jié),令我動容,就是吳京的戰(zhàn)友拿出他閨女...
    麻麻爸爸閱讀 430評論 1 2
  • 不念過往, 不畏將來。 是個狀態(tài)。 做得到繼續(xù), 做不到倒地。
    lapetite33明天在哪里閱讀 221評論 0 0
  • 你,是幾個群的群主?很多人會是不止一個。 但,有幾個群你覺得很活躍,甚至從群里變現(xiàn)了?很多人都是0,甚至是負(fù)(投入...
    哎賣姜閱讀 6,733評論 2 8
  • 日復(fù)一日,我們尋求亞里士多德在《倫理學(xué)》中提出的那一古老問題的答案:一個人應(yīng)該如何度過他的一生?但,問題的答案總在...
    畫重點閱讀 746評論 0 1

友情鏈接更多精彩內(nèi)容