using System;
          using System.Data;
          using System.Configuration;
          using System.Collections;
          using System.Web;
          using System.Web.Security;
          using System.Web.UI;
          using System.Web.UI.WebControls;
          using System.Web.UI.WebControls.WebParts;
          using System.Web.UI.HtmlControls;
          using System.Text.RegularExpressions;
          using System.Collections;
          using System.IO;
          using System.Net;
          using System.Text;
          namespace NewsCollection
          {
              public partial class SiteEdit : System.Web.UI.Page
              {
                  string urlData = "";
                  protected void Page_Load(object sender, EventArgs e)
                  {
                      this.BtCollection.Attributes.Add("onclick", "Status.showInfo('加載中');");
                      if (!Page.IsPostBack)
                      {
                          AjaxAction();
                          BindData();
                      }
                  
                  }
                  public string GetRequest(string key)
                  {
                      key = Convert.ToString(Request[key]??"");
                      key = key==null?(""):(key);
                      return key;
                  }
                  public void AjaxAction()
                  {
                      string isAjax = GetRequest("isAjax").ToLower();
                      if (isAjax == "true")
                      {
                          string state = "";
                          string action = this.GetRequest("action").ToLower();
                          string values = this.GetRequest("values");
                          if (action == "newsbody")
                          {
                              string modelstart = this.GetRequest("modelstart");
                              string modelend = this.GetRequest("modelend");
                              string modelbody = this.GetRequest("modelbody");
                              string siteUrl = this.GetRequest("siteUrl");
                              ArrayList al = this.GetModelData(modelstart, modelend, modelbody, siteUrl);
                              StringBuilder sb = new StringBuilder();
                              foreach (string s in al)
                              {
                                  sb.Append(s);
                              }
                              state = sb.ToString();
                          }
                          else if (action == "newsdetail")
                          {
                              string modelstart = this.GetRequest("modelstart");
                              string modelend = this.GetRequest("modelend");
                              string modelbody = this.GetRequest("modelbody");
                              string siteUrl = this.GetRequest("siteUrl");
                              string newsTitleStart = this.GetRequest("newsTitleStart");
                              string newsTitleEnd = this.GetRequest("newsTitleEnd");
                              string newsContentStart = this.GetRequest("newsContentStart");
                              string newsContentEnd = this.GetRequest("newsContentEnd");
                              state = GetNews(GetModelData(modelstart, modelend, modelbody, siteUrl), newsTitleStart, newsTitleEnd, newsContentStart, newsContentEnd);
                          }
                          else
                          {
                              state = "test Ajax";
                          }
                          Response.Clear();
                          Response.Write(state);
                          Response.End();
                      }
                  }
                  public void BindData()
                  {
                      string Gid = Convert.ToString(Request["Gid"] ?? "");
                      if (Gid.Length > 0)
                      {
                          Beans.Sites sites = new Beans.Sites();
                          sites.Gid = Gid;
                          sites = sites.SelectById();
                          TbSiteName.Text  = sites.SiteName;
                          TbSiteUrl.Text =sites.SiteUrl;
                          TbSiteModelStart.Text = sites.SiteModelStart;
                          TbSiteModelEnd.Text =sites.SiteModelEnd;
                          TbSiteModelBody.Text = sites.SiteModelBody;
                          TbNewsTitleStart.Text =sites.NewsTitleStart;
                          TbNewsTitleEnd.Text =sites.NewsTitleEnd;
                          TbNewsContentStart.Text =sites.NewsContentStart;
                          TbNewsContentEnd.Text =sites.NewsContentEnd;
                      }
                  }
                  protected void BtEdit_Click(object sender, EventArgs e)
                  {
                      string message = "系統(tǒng)錯誤請重試";
                      string script = "history.go(-1)";
                      string Gid = Convert.ToString(Request["Gid"]??"");
                      Beans.Sites sites = new Beans.Sites();
                      sites.SiteName = TbSiteName.Text.Trim();
                      sites.SiteUrl = TbSiteUrl.Text.Trim();
                      sites.SiteModelStart = TbSiteModelStart.Text.Trim();
                      sites.SiteModelEnd = TbSiteModelEnd.Text.Trim();
                      sites.SiteModelBody = TbSiteModelBody.Text.Trim();
                      sites.NewsTitleStart = TbNewsTitleStart.Text.Trim();
                      sites.NewsTitleEnd = TbNewsTitleEnd.Text.Trim();
                      sites.NewsContentStart = TbNewsContentStart.Text.Trim();
                      sites.NewsContentEnd = TbNewsContentEnd.Text.Trim();
                      if (Gid.Length > 0)
                      {
                          sites.Gid=Gid;
                          if (sites.Update())
                          {
                              message = "修改成功!";
                          }
                       
                      }
                      else
                      {
                          if (sites.Add())
                          {
                              message = "添加成功!";
                          }
                        
                      }
                      Response.Write("<script type='text/javascript'>alert('"+message+"');"+script+"</script>");
                   
                  }

                  protected void BtCollection_Click(object sender, EventArgs e)
                  {
                      GetNews(GetModelData(TbSiteModelStart.Text.Replace("(", "\\(").Replace(")", "\\)").Replace(".", "\\."), TbSiteModelEnd.Text.Replace("(", "\\(").Replace(")", "\\)").Replace(".", "\\."), TbSiteModelBody.Text, TbSiteUrl.Text), TbNewsTitleStart.Text.Trim(), TbNewsTitleEnd.Text.Trim(), TbNewsContentStart.Text.Trim(), TbNewsContentEnd.Text.Trim());
               
                     // GetNews(GetModelData(TbSiteModelStart.Text.Replace("(", "\\(").Replace(")", "\\)").Replace(".", "\\."), TbSiteModelEnd.Text.Replace("(", "\\(").Replace(")", "\\)").Replace(".", "\\."), TbSiteModelBody.Text, TbSiteUrl.Text));
                  }
                  public ArrayList GetModelData(string modelstart,string modelend,string modelbody,string SiteUrl)
                  {

                      ArrayList al = new ArrayList();
                      string content = GetHttpData(SiteUrl,"gb2312");
                      Regex reg = new Regex(modelstart+"(?<newsBody>[\\s\\S]*)"+modelend, RegexOptions.IgnoreCase | RegexOptions.Multiline);
                      Match mat = reg.Match(content);
                      //TbContent.Text = mat.Groups["newsBody"].Value.ToString();
                      Regex regurl = new Regex(modelbody.Replace("_url_", "(?<url>[^\"'\\s]+)"), RegexOptions.IgnoreCase | RegexOptions.Singleline);
                      Match maturl = regurl.Match(mat.Groups["newsBody"].Value.ToString());
                      while (maturl.Success)
                      {
                          //z-zA-A0-9/\\.:
                          string temp = maturl.Groups["url"].Value;
                          al.Add(temp);
                          Response.Write(temp.StartsWith("http://") ? (temp) : (temp.Insert(0, "http://" + SiteUrl.Replace("http://","").Substring(0, SiteUrl.LastIndexOf("/")))) + "<br>");
                          maturl = maturl.NextMatch();
                      }
                      return  al;
                  }
              
                  public string  GetNews(ArrayList al,string titleStart,string titleEnd,string contentStart,string contetnEnd)
                  {
                      StringBuilder sb = new StringBuilder();
                      if (al != null)
                      {
                      
                          foreach (string s in al)
                          {

                              Regex reg = new Regex(titleStart.Replace("(", "\\(").Replace(")", "\\)") + "(?<title>[^<]*)" + titleEnd.Replace("(", "\\(").Replace(")", "\\)") + "[\\s\\S]+" + contentStart.Replace("(", "\\(").Replace(")", "\\)") + "(?<content>[\\s\\S]+)" + contetnEnd.Replace("(", "\\(").Replace(")", "\\)"), RegexOptions.IgnoreCase | RegexOptions.Multiline);
                              Match mat = reg.Match(GetHttpData(s, "gb2312"));
                              //Response.Write(string.Format("news:{0}<br>content:{1}<br>",mat.Groups["title"].Value,mat.Groups["content"].Value));
                              Beans.News news = new Beans.News();
                              news.Title = mat.Groups["title"].Value;
                              news.Typeid = Convert.ToString(Request["Gid"]);//mat.Groups["title"].Value;
                              news.From = this.TbSiteName.Text;//mat.Groups["content"].Value;
                              news.Content = mat.Groups["content"].Value;
                              news.Add();
                              sb.AppendFormat("title:{0}", mat.Groups["title"].Value);
                          }
                      }
                      return sb.ToString();
                  }
                  public string GetHttpData(string sUrl, string encoding)
                  {
                      string sRslt = null;
                      WebResponse oWebRps = null;
                      WebRequest oWebRqst = WebRequest.Create(sUrl);
                      oWebRqst.Timeout = 50000;
                      try
                      {
                          oWebRps = oWebRqst.GetResponse();
                      }

                      finally
                      {
                          if (oWebRps != null)
                          {
                              StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), System.Text.Encoding.GetEncoding(encoding));
                              sRslt = oStreamRd.ReadToEnd();
                              oStreamRd.Close();
                              oWebRps.Close();
                          }
                      }
                      return sRslt;
                  }

              

             
              }
          }

           

          posted on 2009-07-18 15:08 sanmao 閱讀(238) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 海阳市| 定州市| 商水县| 文安县| 油尖旺区| 晋州市| 封丘县| 和硕县| 汉源县| 甘孜| 常州市| 满城县| 南靖县| 平舆县| 陆河县| 黔南| 台中县| 华亭县| 临汾市| 扎兰屯市| 桦川县| 林甸县| 安义县| 固阳县| 鸡泽县| 和硕县| 合川市| 稷山县| 普定县| 桂东县| 神池县| 理塘县| 翼城县| 德化县| 巩留县| 房产| 蓬莱市| 舞钢市| 万载县| 孝义市| 罗定市|