2019-03-26

<%@ WebHandler Language="C#" Class="server" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using Model;
using System.Text.RegularExpressions;

public class server : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        try
        {
            //获取调用该页面的“方法名”
            string MethodName = HttpContext.Current.Request.PathInfo.Substring(1);
            //通过方法名找到该方法对象
            System.Reflection.MethodInfo method = this.GetType().GetMethod(MethodName);
            if (method != null)
                //反射
                HttpContext.Current.Response.Write(method.Invoke(this, null));
        }
        catch (Exception)
        {

            HttpContext.Current.Response.Write("该服务不显示对外开放,需提供需要调用的方法和参数");
        }
    }
    //获取所有栏目
    public string GetColumn()
    {
        string code = HttpContext.Current.Request.QueryString["code"];
        //16位MD5加密wuhansiwen
        if (code != null && code.Trim().ToUpper() == "5666DFA2C1D11E39")
        {
            string sql = "select * from columnInfo";
            List<columnInfo> list = new List<columnInfo>();
            try
            {
                using (SqlDataReader dr = sysSqlHelp.SqlHelp.ExecuteReader(sql, System.Data.CommandType.Text))
                {
                    while (dr.Read())
                    {
                        columnInfo Model = new columnInfo();
                        Model.cid = Convert.ToInt32(dr["cid"]);
                        Model.columnNmae = dr["columnNmae"].ToString();
                        Model.note = dr["note"].ToString();
                        list.Add(Model);
                    }
                }
            }
            catch (Exception)
            { }
            string xml = Help.XmlSerialize<List<columnInfo>>(list);
            return xml;
        }
        else
        {
            return "非法请求";
        }
    }
    //根据栏目获取文章列表
    public string GetArticleBycid()
    {
        string code = HttpContext.Current.Request.QueryString["code"];
        string cid = HttpContext.Current.Request.QueryString["cid"];
        string index = HttpContext.Current.Request.QueryString["index"];
        string count = HttpContext.Current.Request.QueryString["count"];
        //16位MD5加密wuhansiwen
        if (code != null && code.Trim().ToUpper() == "5666DFA2C1D11E39" && cid != null && index != null && count != null)
        {
            string sql = "select top " + count + " * from articleInfo where cid=@cid and articleType=1 and id not in(select top " + index + " id from articleInfo where cid=@cid and articleType=1 order by articleTime desc) order by articleTime desc";
            SqlParameter[] sqlp = new SqlParameter[] {
                new SqlParameter("@cid",cid),
            };
            List<articleTitleInfo> list = new List<articleTitleInfo>();
            try
            {
                using (SqlDataReader dr = sysSqlHelp.SqlHelp.ExecuteReader(sql, System.Data.CommandType.Text, sqlp))
                {
                    while (dr.Read())
                    {
                        articleTitleInfo Model = new articleTitleInfo();
                        Model.articleTitle = dr["articleTitle"].ToString();
                        Model.id = Convert.ToInt32(dr["id"]);
                        Model.articleTime = Convert.ToDateTime(dr["articleTime"]);
                        string[] imgs = GetHtmlImageUrlList(dr["articleContent"].ToString());

                        if (imgs.Length > 0)
                        {
                            if (imgs[0].Trim().Length > 7 && imgs[0].Trim().Substring(0, 7).ToLower() == "http://")
                            {
                                Model.imgUrl = imgs[0].Trim();
                            }
                            else
                            {
                                //处理
                                string url = HttpContext.Current.Request.Url.AbsoluteUri;
                                url = url.Substring(0, url.LastIndexOf("webserver"));
                                string sa = imgs[0].Trim();
                                if (sa.ToLower().Trim().Substring(0, 1).ToCharArray()[0] > 96 && sa.ToLower().Trim().Substring(0, 1).ToCharArray()[0] < 123)
                                {
                                    //不需要转换
                                    Model.imgUrl = url + sa;
                                }
                                else
                                {
                                    Model.imgUrl = url + sa.Substring(sa.IndexOf("/") + 1);
                                }
                            }
                        }
                        else
                        {
                            Model.imgUrl = "";
                        }
                        //文字处
                        string co = StripHT(dr["articleContent"].ToString());
                        if (co.Trim().Length > 30)
                        {
                            co = co.Substring(0, 30);
                        }
                        Model.articleContent = co;
                        list.Add(Model);
                    }
                }
            }
            catch (Exception)
            { }
            string xml = Help.XmlSerialize<List<articleTitleInfo>>(list);
            return xml;
        }
        else
        {
            return "非法请求";
        }
    }
    //根据栏目获取文章列表(图片)
    public string GetImagArticleList()
    {
        string code = HttpContext.Current.Request.QueryString["code"];
        string cid = HttpContext.Current.Request.QueryString["cid"];
        string index = HttpContext.Current.Request.QueryString["index"];
        string count = HttpContext.Current.Request.QueryString["count"];
        
        try
        {
            Convert.ToInt32(index);
            Convert.ToInt32(count);
        }
        catch (Exception)
        {

            return "非法请求";
        }
        //16位MD5加密wuhansiwen
        if (code != null && code.Trim().ToUpper() == "5666DFA2C1D11E39" && cid !=null&& index != null && count != null)
        {
            string sql = "select top " + count + " * from articleInfo where articleType=2 and cid=" + cid + " and id not in(select top " + index + " id from articleInfo where  articleType=2 and cid=" + cid + " order by articleTime desc) order by articleTime desc";
            List<ImagearticleInfo> list = new List<ImagearticleInfo>();
            try
            {
                using (SqlDataReader dr = sysSqlHelp.SqlHelp.ExecuteReader(sql, System.Data.CommandType.Text))
                {
                    while (dr.Read())
                    {
                        ImagearticleInfo Model = new ImagearticleInfo();
                        Model.articleTitle = dr["articleTitle"].ToString();
                        Model.id = Convert.ToInt32(dr["id"]);
                        Model.articleTime = Convert.ToDateTime(dr["articleTime"]);
                        string[] imgs = GetHtmlImageUrlList(dr["articleContent"].ToString());
                        Model.imgCount = imgs.Length;
                        //第一张图
                        if (imgs.Length > 0)
                        {
                            if (imgs[0].Trim().Length > 7 && imgs[0].Trim().Substring(0, 7).ToLower() == "http://")
                            {
                                Model.imgUrl1 = imgs[0].Trim();
                            }
                            else
                            {
                                //处理
                                Model.imgUrl1 = GetImgUrlHtpp(imgs[0]);
                            }
                        }
                        else
                        {
                            Model.imgUrl1 = "";
                        }
                        //第二章图片
                        if (imgs.Length > 1)
                        {
                            if (imgs[1].Trim().Length > 7 && imgs[1].Trim().Substring(0, 7).ToLower() == "http://")
                            {
                                Model.imgUrl2 = imgs[1].Trim();
                            }
                            else
                            {
                                //处理
                                Model.imgUrl2 = GetImgUrlHtpp(imgs[1]);
                            }
                        }
                        else
                        {
                            Model.imgUrl2 = "";
                        }
                        //文字处
                        string co = StripHT(dr["articleContent"].ToString());
                        if (co.Trim().Length > 30)
                        {
                            co = co.Substring(0, 30);
                        }
                        //Model.articleContent = co;
                        list.Add(Model);
                    }
                }
            }
            catch (Exception)
            { }
            string xml = Help.XmlSerialize<List<ImagearticleInfo>>(list);
            return xml;
        }
        else
        {
            return "非法请求";
        }
    }
    public string GetImgUrlHtpp(string http)
    {
        string url = HttpContext.Current.Request.Url.AbsoluteUri;
        url = url.Substring(0, url.LastIndexOf("webserver"));
        string sa = http.Trim();
        if (sa.ToLower().Trim().Substring(0, 1).ToCharArray()[0] > 96 && sa.ToLower().Trim().Substring(0, 1).ToCharArray()[0] < 123)
        {
            //不需要转换
            return url + sa;
        }
        else
        {
            return url + sa.Substring(sa.IndexOf("/") + 1);
        }
    }
    //提取html中的文本
    private string StripHT(string Htmlstring)  //从html中提取纯文本
    {
        //删除脚本 
        Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
        //删除HTML 
        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);", "\xa1", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
        Htmlstring.Replace("<", "");
        Htmlstring.Replace(">", "");
        Htmlstring.Replace("\r\n", "");

        return Htmlstring;
    }
    public string[] GetHtmlImageUrlList(string sHtmlText)
    {
        // 定义正则表达式用来匹配 img 标签   
        Regex regImg = 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);

        // 搜索匹配的字符串   
        MatchCollection matches = regImg.Matches(sHtmlText);
        int i = 0;
        string[] sUrlList = new string[matches.Count];

        // 取得匹配项列表   
        foreach (Match match in matches)
            sUrlList[i++] = match.Groups["imgUrl"].Value;
        return sUrlList;
    }
    //根据栏目获取文章
    public string GetArticleById()
    {
        string code = HttpContext.Current.Request.QueryString["code"];
        string id = HttpContext.Current.Request.QueryString["id"];
        //16位MD5加密wuhansiwen
        if (code != null && code.Trim().ToUpper() == "5666DFA2C1D11E39" && id != null)
        {
            string sql = "select *,(select columnNmae from columnInfo where columnInfo.cid=articleInfo.cid) as cname from articleInfo,userInfo where articleInfo.uid=userInfo.uid and id=@id order by articleTime desc";
            SqlParameter sqlp = new SqlParameter("@id", id);
            articleInfo Model = new articleInfo();
            try
            {
                using (SqlDataReader dr = sysSqlHelp.SqlHelp.ExecuteReader(sql, System.Data.CommandType.Text, sqlp))
                {
                    while (dr.Read())
                    {
                        Model.articleAuthor = dr["articleAuthor"].ToString();
                        Model.articleContent = dr["articleContent"].ToString();
                        Model.articleSource = dr["articleSource"].ToString();
                        Model.articleTime = Convert.ToDateTime(dr["articleTime"]);
                        Model.articleTitle = dr["articleTitle"].ToString();
                        Model.articleType = Convert.ToInt32(dr["articleType"]);
                        Model.cid = Convert.ToInt32(dr["cid"]);
                        Model.cname = dr["cname"].ToString();
                        Model.clickCount = Convert.ToInt32(dr["clickCount"]);
                        Model.id = Convert.ToInt32(dr["id"]);
                        Model.uid = Convert.ToInt32(dr["uid"]);
                        Model.uname = dr["uname"].ToString();
                        Model.indexIn = Convert.ToInt32(dr["indexIn"]);
                    }
                }
            }
            catch (Exception)
            { }
            string xml = Help.XmlSerialize<articleInfo>(Model);
            return xml;
        }
        else
        {
            return "非法请求";
        }
    }
    //获取轮换新闻
    public string GetRotateArticle()
    {
        string code = HttpContext.Current.Request.QueryString["code"];
        //16位MD5加密wuhansiwen
        if (code != null && code.Trim().ToUpper() == "5666DFA2C1D11E39")
        {
            string sql = "select top 5 * from rotateArticle order by id desc";
            List<rotateArticle> list = new List<rotateArticle>();
            try
            {
                using (SqlDataReader dr = sysSqlHelp.SqlHelp.ExecuteReader(sql, System.Data.CommandType.Text))
                {
                    while (dr.Read())
                    {
                        rotateArticle Model = new rotateArticle();
                        //Model.articleUrl = dr["articleUrl"].ToString();
                        if (dr["articleUrl"].ToString().Trim().Length > 7 && dr["articleUrl"].ToString().Trim().Substring(0, 7).ToLower() == "http://")
                        {
                            Model.articleUrl = dr["articleUrl"].ToString().Trim();
                        }
                        else
                        {
                            //处理
                            Model.articleUrl = GetImgUrlHtpp(dr["articleUrl"].ToString());
                        }
                        Model.id = Convert.ToInt32(dr["id"]);
                        Model.imageTitle = dr["imageTitle"].ToString();
                        //Model.imageUrl = dr["imageUrl"].ToString();
                        if (dr["imageUrl"].ToString().Trim().Length > 7 && dr["imageUrl"].ToString().Trim().Substring(0, 7).ToLower() == "http://")
                        {
                            Model.imageUrl = dr["imageUrl"].ToString().Trim();
                        }
                        else
                        {
                            //处理
                            Model.imageUrl = GetImgUrlHtpp(dr["imageUrl"].ToString());
                        }
                        if (dr["articleUrlPhone"].ToString().Trim().Length > 7 && dr["articleUrlPhone"].ToString().Trim().Substring(0, 7).ToLower() == "http://")
                        {
                            Model.articleUrlPhone = dr["articleUrlPhone"].ToString().Trim();
                        }
                        else
                        {
                            //处理
                            Model.articleUrlPhone = GetImgUrlHtpp(dr["articleUrlPhone"].ToString());
                        }
                        //Model.articleUrlPhone = dr["articleUrlPhone"].ToString();
                        list.Add(Model);
                    }
                }
            }
            catch (Exception)
            { }
            string xml = Help.XmlSerialize<List<rotateArticle>>(list);
            return xml;
        }
        else
        {
            return "非法请求";
        }
    }
    //------------------------------------------------------------意见的提交-
    //测试意见
    public string PostYijian()
    {
        string txt = HttpContext.Current.Request.Form["content"].ToString();
        string code = HttpContext.Current.Request.Form["code"].ToString();
        if (code == "5666DFA2C1D11E39" && txt.Trim().Length > 0)
        {
            string sql = "insert into opinionInfo(opinionContent,userIp) values(@opinionContent,@userIp)";
            try
            {
                SqlParameter[] sqlp = new SqlParameter[]{
                new SqlParameter("@opinionContent", txt),
                new SqlParameter("@userIp", GetMacHelp.GetIp(HttpContext.Current.Request))
                };
                if (sysSqlHelp.SqlHelp.ExecuteNonQuery(sql, CommandType.Text, sqlp) > 0)
                {
                    return "1";
                }
                else
                {
                    return "0";
                }
            }
            catch (Exception)
            {
                return "2";
            }
        }
        else
        {
            return "3";
        }
    }
    //-------------------------------------------------------------检察长信箱
    public string EmailSent()
    {
        string emalil = HttpContext.Current.Request.Form["emalil"].ToString();
        string theme = HttpContext.Current.Request.Form["theme"].ToString();
        string content = HttpContext.Current.Request.Form["content"].ToString();
        string code = HttpContext.Current.Request.Form["code"].ToString();
        if (code != "5666DFA2C1D11E39")
        {
            return "3";
        }
        try
        {
            string sql = "insert into EmailSent(Emalil, theme, [content],uip) values(@Emalil, @theme, @content,@uip)";
            SqlParameter[] sqlps = new SqlParameter[] {
            new SqlParameter("@Emalil",emalil),
            new SqlParameter("@theme",theme),
            new SqlParameter("@content",content),
            new SqlParameter("@uip",GetMacHelp.GetIp(HttpContext.Current.Request))
        };
            if (sysSqlHelp.SqlHelp.ExecuteNonQuery(sql, CommandType.Text, sqlps) > 0)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }
        catch (Exception)
        {

            return "2";
        }
    }
    //------------------------------------------------------法律资讯
    public string lawSent()
    {
        string emalil = HttpContext.Current.Request.Form["emalil"].ToString();
        string theme = HttpContext.Current.Request.Form["theme"].ToString();
        string content = HttpContext.Current.Request.Form["content"].ToString();
        string code = HttpContext.Current.Request.Form["code"].ToString();
        if (code != "5666DFA2C1D11E39")
        {
            return "3";
        }
        try
        {
            string sql = "insert into lawSent(Emalil, theme, [content],uip) values(@Emalil, @theme, @content,@uip)";
            SqlParameter[] sqlps = new SqlParameter[] {
            new SqlParameter("@Emalil",emalil),
            new SqlParameter("@theme",theme),
            new SqlParameter("@content",content),
            new SqlParameter("@uip",GetMacHelp.GetIp(HttpContext.Current.Request))
        };
            if (sysSqlHelp.SqlHelp.ExecuteNonQuery(sql, CommandType.Text, sqlps) > 0)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }
        catch (Exception)
        {

            return "2";
        }
    }
    
    //---------------------------------------------------------------图片新闻
    //读取图片文章的具体类容
    //根据栏目获取文章列表(图片)
    public string GetImagArticleById()
    {
        string code = HttpContext.Current.Request.QueryString["code"];
        string id = HttpContext.Current.Request.QueryString["id"];
        //16位MD5加密wuhansiwen
        string xml = "";
        if (code != null && code.Trim().ToUpper() == "5666DFA2C1D11E39" && id != null)
        {
            string sql = "select * from articleInfo,userInfo where articleInfo.uid=userInfo.uid and id=@id order by articleTime desc";
            SqlParameter sqlp = new SqlParameter("@id", id);
            DataTable dt = sysSqlHelp.SqlHelp.GetDataTable(sql, CommandType.Text, sqlp);
            if (dt.Rows.Count > 0)
            {
                string artCount = dt.Rows[0]["articleContent"].ToString();
                string[] img = GetHtmlImageUrlListImgArt(ref artCount);
                artCount = StripHTImgArt(artCount);
                string[] title = artCount.Split('|');
                List<imageArtcle> list = new List<imageArtcle>();
                for (int i = 0; i < img.Length; i++)
                {
                    imageArtcle model = new imageArtcle();
                    model.imgUrl = img[i];
                    if (img[i].Trim().Length > 7 && img[i].Trim().Substring(0, 7).ToLower() == "http://")
                    {
                        model.imgUrl = img[i].Trim();
                    }
                    else
                    {
                        //处理
                        model.imgUrl = GetImgUrlHtpp(img[i]);
                    }
                    if (i < title.Length)
                    {
                        model.imgTitle = title[i];
                    }
                    list.Add(model);
                }
                //创建对象
                imageArtcleInfoPage imagPage = new imageArtcleInfoPage();
                imagPage.articleAuthor = dt.Rows[0]["articleAuthor"].ToString();
                imagPage.imageList = list;
                imagPage.articleSource = dt.Rows[0]["articleSource"].ToString();
                imagPage.articleTime = Convert.ToDateTime(dt.Rows[0]["articleTime"]);
                imagPage.articleTitle = dt.Rows[0]["articleTitle"].ToString();
                imagPage.articleType = Convert.ToInt32(dt.Rows[0]["articleType"]);
                imagPage.cid = Convert.ToInt32(dt.Rows[0]["cid"]);
                imagPage.clickCount = Convert.ToInt32(dt.Rows[0]["clickCount"]);
                imagPage.id = Convert.ToInt32(dt.Rows[0]["id"]);
                imagPage.uid = Convert.ToInt32(dt.Rows[0]["uid"]);
                imagPage.uname = dt.Rows[0]["uname"].ToString();
                imagPage.indexIn = Convert.ToInt32(dt.Rows[0]["indexIn"]);
                xml = Help.XmlSerialize<imageArtcleInfoPage>(imagPage);
            }

        }
        return xml;
    }

    public string GetImagArticleByIdT()
    {
        string code = HttpContext.Current.Request.QueryString["code"];
        string id = HttpContext.Current.Request.QueryString["id"];
        //16位MD5加密wuhansiwen
        string xml = "";
        if (code != null && code.Trim().ToUpper() == "5666DFA2C1D11E39" && id != null)
        {
            string sql = "select * from articleInfo,userInfo where articleInfo.uid=userInfo.uid and id=@id order by articleTime desc";
            SqlParameter sqlp = new SqlParameter("@id", id);
            DataTable dt = sysSqlHelp.SqlHelp.GetDataTable(sql, CommandType.Text, sqlp);
            if (dt.Rows.Count > 0)
            {
                string artCount = dt.Rows[0]["articleContent"].ToString();
                string[] img = GetHtmlImageUrlListImgArt(ref artCount);
                artCount = StripHTImgArt(artCount);
                string[] title = artCount.Split('|');
                List<imageArtcle> list = new List<imageArtcle>();
                for (int i = 0; i < img.Length; i++)
                {
                    imageArtcle model = new imageArtcle();
                    model.imgUrl = img[i];
                    if (img[i].Trim().Length > 7 && img[i].Trim().Substring(0, 7).ToLower() == "http://")
                    {
                        model.imgUrl = img[i].Trim();
                    }
                    else
                    {
                        //处理
                        model.imgUrl = GetImgUrlHtpp(img[i]);
                    }
                    if (i < title.Length)
                    {
                        model.imgTitle = title[i];
                    }
                    list.Add(model);
                }
                //创建对象
                xml = Help.XmlSerialize<List<imageArtcle>>(list);
            }

        }
        return xml;
    }

    //提取html中的文本
    private string StripHTImgArt(string Htmlstring)  //从html中提取纯文本
    {
        //删除脚本 
        Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
        //删除HTML 
        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);", "\xa1", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
        Htmlstring.Replace("<", "");
        Htmlstring.Replace(">", "");
        Htmlstring.Replace("\r\n", "");

        return Htmlstring;
    }
    public string[] GetHtmlImageUrlListImgArt(ref string sHtmlText)
    {
        // 定义正则表达式用来匹配 img 标签   
        Regex regImg = 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);
        // 搜索匹配的字符串   
        MatchCollection matches = regImg.Matches(sHtmlText);
        int i = 0;
        string[] sUrlList = new string[matches.Count];

        // 取得匹配项列表   
        foreach (Match match in matches)
        {
            sUrlList[i++] = match.Groups["imgUrl"].Value;
            sHtmlText = sHtmlText.Replace(match.ToString(), "|");
        }
        return sUrlList;
    }

    //-------------------------------------------------------视频
    public string GetVideoList()
    {
        string code = HttpContext.Current.Request.QueryString["code"];
        string index = HttpContext.Current.Request.QueryString["index"];
        string count = HttpContext.Current.Request.QueryString["count"];
        try
        {
            Convert.ToInt32(index);
            Convert.ToInt32(count);
        }
        catch (Exception)
        {

            return "非法请求";
        }
        string xml = "";
        //16位MD5加密wuhansiwen
        if (code != null && code.Trim().ToUpper() == "5666DFA2C1D11E39" && index != null && count != null)
        {
            string sql = "select top " + count + " * from articleInfo where articleType=3 and id not in(select top " + index + " id from articleInfo where  articleType=3 order by articleTime desc) order by articleTime desc";
            DataTable dt = sysSqlHelp.SqlHelp.GetDataTable(sql, CommandType.Text);
            List<videoInfo> list = new List<videoInfo>();
            foreach (DataRow item in dt.Rows)
            {
                videoInfo model = new videoInfo();
                model.articleTitle = item["articleTitle"].ToString();
                model.articleTime = Convert.ToDateTime(item["articleTime"]);
                string sa = GetImagToStr(item["articleContent"].ToString()).Count > 0 ? GetImagToStr(item["articleContent"].ToString())[0].imgUrl : "";
                if (sa.Trim().Length > 7 && sa.Trim().Substring(0, 7).ToLower() == "http://")
                {
                    model.videoImgUrl = sa.Trim();
                }
                else
                {
                    //处理
                    model.videoImgUrl = GetImgUrlHtpp(sa);
                }
                model.videoUrl = GetImagToStr(item["articleContent"].ToString()).Count > 0 ? GetImagToStr(item["articleContent"].ToString())[0].imgTitle : "#";
                list.Add(model);
            }
            xml = Help.XmlSerialize<List<videoInfo>>(list);
        }
        return xml;

    }

    public List<imageArtcle> GetImagToStr(string html)
    {
        // 定义正则表达式用来匹配 img 标签   
        Regex regImg = 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);

        // 搜索匹配的字符串   
        MatchCollection matches = regImg.Matches(html);
        //string[] sUrlList = new string[matches.Count];
        List<imageArtcle> list = new List<imageArtcle>();
        // 取得匹配项列表   
        foreach (Match match in matches)
        {
            imageArtcle model = new imageArtcle();
            model.imgUrl = match.Groups["imgUrl"].Value;
            string[] title = GetTitle(match.ToString());
            if (title.Length > 0)
            {
                model.imgTitle = title[0];
            }
            //其它属性
            list.Add(model);
        }
        return list;
    }
    public string[] GetTitle(string title)
    {
        // 定义正则表达式用来匹配 img 标签   
        Regex regImg = new Regex(@"<img\b[^<>]*?\btitle[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);

        // 搜索匹配的字符串   
        MatchCollection matches = regImg.Matches(title);
        int i = 0;
        string[] sUrlList = new string[matches.Count];

        // 取得匹配项列表   
        foreach (Match match in matches)
        {
            sUrlList[i++] = match.Groups["imgUrl"].Value;
        }
        return sUrlList;
    }
    /// <summary>
    /// ////////////////////////////////////////////////////////////////////专题
    /// </summary>
    /// //获取专题列表
    public string GetsubjectInfoList() 
    {
        string code = HttpContext.Current.Request.QueryString["code"];
        //16位MD5加密wuhansiwen
        if (code != null && code.Trim().ToUpper() == "5666DFA2C1D11E39")
        {
            string sql = "select * from subjectInfo order by SubTime desc";
            List<subjectInfo> list = new List<subjectInfo>();
            try
            {
                using (SqlDataReader dr = sysSqlHelp.SqlHelp.ExecuteReader(sql, System.Data.CommandType.Text))
                {
                    while (dr.Read())
                    {
                        subjectInfo Model = new subjectInfo();
                        Model.sid = Convert.ToInt32(dr["sid"]);
                        Model.indexIn = Convert.ToInt32(dr["indexIn"]);
                        Model.SubImgUrl = dr["SubImgUrl"].ToString();
                        if (Model.SubImgUrl.Trim().Length > 7 && Model.SubImgUrl.Trim().Substring(0, 7).ToLower() == "http://")
                        {
                            Model.SubImgUrl = Model.SubImgUrl.Trim();
                        }
                        else
                        {
                            //处理
                            Model.SubImgUrl = GetImgUrlHtpp(Model.SubImgUrl);
                        }
                        Model.SubLogUrl = dr["SubLogUrl"].ToString();
                        if (Model.SubLogUrl.Trim().Length > 7 && Model.SubLogUrl.Trim().Substring(0, 7).ToLower() == "http://")
                        {
                            Model.SubLogUrl = Model.SubLogUrl.Trim();
                        }
                        else
                        {
                            //处理
                            Model.SubLogUrl = GetImgUrlHtpp(Model.SubLogUrl);
                        }
                        Model.SubName = dr["SubName"].ToString();
                        Model.SubTime = Convert.ToDateTime(dr["SubTime"]);
                        list.Add(Model);
                    }
                }
            }
            catch (Exception)
            { }
            string xml = Help.XmlSerialize<List<subjectInfo>>(list);
            return xml;
        }
        else
        {
            return "非法请求";
        }
    }
    //获取专题下的所有栏目
    public string GetsubColumn()
    {
        string code = HttpContext.Current.Request.QueryString["code"];
        string sid = HttpContext.Current.Request.QueryString["sid"];
        //16位MD5加密wuhansiwen
        if (code != null && code.Trim().ToUpper() == "5666DFA2C1D11E39"&&sid!=null)
        {
            string sql = "select * from subColumn where sid=@sid";
            SqlParameter sqlp = new SqlParameter("@sid",sid);
            List<subColumn> list = new List<subColumn>();
            try
            {
                using (SqlDataReader dr = sysSqlHelp.SqlHelp.ExecuteReader(sql, System.Data.CommandType.Text, sqlp))
                {
                    while (dr.Read())
                    {
                        subColumn Model = new subColumn();
                        Model.sid = Convert.ToInt32(dr["sid"]);
                        Model.note = dr["sid"].ToString();
                        Model.cid = Convert.ToInt32(dr["cid"]);
                        Model.SubCName = dr["SubCName"].ToString();
                        list.Add(Model);
                    }
                }
            }
            catch (Exception)
            { }
            string xml = Help.XmlSerialize<List<subColumn>>(list);
            return xml;
        }
        else
        {
            return "非法请求";
        }
    }
    //获取专题中的文章
    public string GetsubArticleList()
    {
        string code = HttpContext.Current.Request.QueryString["code"];
        string sid = HttpContext.Current.Request.QueryString["sid"];
        //16位MD5加密wuhansiwen
        if (code != null && code.Trim().ToUpper() == "5666DFA2C1D11E39" && sid != null)
        {
            string sql = "select * from subColumn where sid=@sid";
            SqlParameter sqlp = new SqlParameter("@sid", sid);
            //List<subColumn> list = new List<subColumn>();
            List<SubArticle> list = new List<SubArticle>();
            try
            {
                using (SqlDataReader dr = sysSqlHelp.SqlHelp.ExecuteReader(sql, System.Data.CommandType.Text, sqlp))
                {
                    while (dr.Read())
                    {
                        //subColumn Model = new subColumn();
                        //Model.sid = Convert.ToInt32(dr["sid"]);
                        //Model.note = dr["sid"].ToString();
                        //Model.cid = Convert.ToInt32(dr["cid"]);
                        //Model.SubCName = dr["SubCName"].ToString();
                        //list.Add(Model);
                        SubArticle Mode=new SubArticle();
                        Mode.sid=Convert.ToInt32(dr["sid"]);
                        Mode.SubCName = dr["SubCName"].ToString();
                        Mode.list = GetsubArticleBycid(Convert.ToInt32(dr["cid"]).ToString());
                        list.Add(Mode);
                        
                    }
                }
            }
            catch (Exception)
            { }
            string xml = Help.XmlSerialize<List<SubArticle>>(list);
            return xml;
        }
        else
        {
            return "非法请求";
        }
    }
    //根据栏目获取文章
    //根据栏目获取文章列表
    public string GetsubArticleBycid()
    {
        string code = HttpContext.Current.Request.QueryString["code"];
        string cid = HttpContext.Current.Request.QueryString["cid"];
        string index = HttpContext.Current.Request.QueryString["index"];
        string count = HttpContext.Current.Request.QueryString["count"];
        //16位MD5加密wuhansiwen
        if (code != null && code.Trim().ToUpper() == "5666DFA2C1D11E39" && cid != null && index != null && count != null)
        {
            string sql = "select top " + count + " * from subArticle where cid=@cid and articleType=1 and id not in(select top " + index + " id from subArticle where cid=@cid and articleType=1 order by articleTime desc) order by articleTime desc";
            SqlParameter[] sqlp = new SqlParameter[] {
                new SqlParameter("@cid",cid),
            };
            List<articleTitleInfo> list = new List<articleTitleInfo>();
            try
            {
                using (SqlDataReader dr = sysSqlHelp.SqlHelp.ExecuteReader(sql, System.Data.CommandType.Text, sqlp))
                {
                    while (dr.Read())
                    {
                        articleTitleInfo Model = new articleTitleInfo();
                        Model.articleTitle = dr["articleTitle"].ToString();
                        Model.id = Convert.ToInt32(dr["id"]);
                        Model.articleTime = Convert.ToDateTime(dr["articleTime"]);
                        string[] imgs = GetHtmlImageUrlList(dr["articleContent"].ToString());

                        if (imgs.Length > 0)
                        {
                            if (imgs[0].Trim().Length > 7 && imgs[0].Trim().Substring(0, 7).ToLower() == "http://")
                            {
                                Model.imgUrl = imgs[0].Trim();
                            }
                            else
                            {
                                //处理
                                string url = HttpContext.Current.Request.Url.AbsoluteUri;
                                url = url.Substring(0, url.LastIndexOf("webserver"));
                                string sa = imgs[0].Trim();
                                if (sa.ToLower().Trim().Substring(0, 1).ToCharArray()[0] > 96 && sa.ToLower().Trim().Substring(0, 1).ToCharArray()[0] < 123)
                                {
                                    //不需要转换
                                    Model.imgUrl = url + sa;
                                }
                                else
                                {
                                    Model.imgUrl = url + sa.Substring(sa.IndexOf("/") + 1);
                                }
                            }
                        }
                        else
                        {
                            Model.imgUrl = "";
                        }
                        //文字处
                        string co = StripHT(dr["articleContent"].ToString());
                        if (co.Trim().Length > 30)
                        {
                            co = co.Substring(0, 30);
                        }
                        Model.articleContent = co;
                        list.Add(Model);
                    }
                }
            }
            catch (Exception)
            { }
            string xml = Help.XmlSerialize<List<articleTitleInfo>>(list);
            return xml;
        }
        else
        {
            return "非法请求";
        }
    }

    //根据栏目获取文章列表
    public List<articleTitleInfo>  GetsubArticleBycid(string cid)
    {
        //16位MD5加密wuhansiwen
        if (cid != null)
        {
            string sql = "select * from subArticle where cid=@cid and articleType=1 order by articleTime desc";
            SqlParameter[] sqlp = new SqlParameter[] {
                new SqlParameter("@cid",cid),
            };
            List<articleTitleInfo> list = new List<articleTitleInfo>();
            try
            {
                using (SqlDataReader dr = sysSqlHelp.SqlHelp.ExecuteReader(sql, System.Data.CommandType.Text, sqlp))
                {
                    while (dr.Read())
                    {
                        articleTitleInfo Model = new articleTitleInfo();
                        Model.articleTitle = dr["articleTitle"].ToString();
                        Model.id = Convert.ToInt32(dr["id"]);
                        Model.articleTime = Convert.ToDateTime(dr["articleTime"]);
                        string[] imgs = GetHtmlImageUrlList(dr["articleContent"].ToString());

                        if (imgs.Length > 0)
                        {
                            if (imgs[0].Trim().Length > 7 && imgs[0].Trim().Substring(0, 7).ToLower() == "http://")
                            {
                                Model.imgUrl = imgs[0].Trim();
                            }
                            else
                            {
                                //处理
                                string url = HttpContext.Current.Request.Url.AbsoluteUri;
                                url = url.Substring(0, url.LastIndexOf("webserver"));
                                string sa = imgs[0].Trim();
                                if (sa.ToLower().Trim().Substring(0, 1).ToCharArray()[0] > 96 && sa.ToLower().Trim().Substring(0, 1).ToCharArray()[0] < 123)
                                {
                                    //不需要转换
                                    Model.imgUrl = url + sa;
                                }
                                else
                                {
                                    Model.imgUrl = url + sa.Substring(sa.IndexOf("/") + 1);
                                }
                            }
                        }
                        else
                        {
                            Model.imgUrl = "";
                        }
                        //文字处
                        string co = StripHT(HttpUtility.HtmlDecode(dr["articleContent"].ToString()));
                        co = co.Replace(" ", "");
                        if (co.Trim().Length > 60)
                        {
                            co = co.Substring(0, 60);
                        }
                        Model.articleContent = co;
                        list.Add(Model);
                    }
                }
            }
            catch (Exception)
            { }
            return list;
        }
        else
        {
            return null;
        }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,875评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,569评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,475评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,459评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,537评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,563评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,580评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,326评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,773评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,086评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,252评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,921评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,566评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,190评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,435评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,129评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,125评论 2 352

推荐阅读更多精彩内容