public static Dictionary <string, string> GetForumContent(string url) { Dictionary <string, string> dic = new Dictionary <string, string>() { { "title", "" }, { "user", "" } }; if (url.IndexOf("www.i200.cn/bbs") < 0) { return(null);//不是合法地址 } //制定编码格式 var encoding = Encoding.GetEncoding("utf-8"); //url的设置 var request = (HttpWebRequest)WebRequest.Create(url); //设置请求的方式 request.Method = "Get"; //设置Content-Type 的值 //request.ContentType = "application/x-www-form-urlencoded"; //request.ContentLength = data.Length; //var outStream = request.GetRequestStream(); //outStream.Write(data, 0, data.Length); //outStream.Close(); var response = (HttpWebResponse)request.GetResponse(); var srContent = new System.IO.StreamReader(response.GetResponseStream(), encoding); //获取抓取下来的页面内容 var strPage = srContent.ReadToEnd(); response.Close(); srContent.Close(); //获取帖子标题 Regex regTitle = new Regex(@"(?i)(?<=<a.*?id=""thread_subject"".*?>)[^<]+(?=</a>)"); MatchCollection mcTitle = regTitle.Matches(strPage); //获取帖子发表时间 Regex regTime = new Regex(@"(?i)(?<=<em.*?id=""authorposton.*?>发表于)[^<]+(?=</em>)"); MatchCollection mcTime = regTime.Matches(strPage); if (mcTime.Count == 0) { regTime = new Regex(@"(?i)(?<=<em.*?id=""authorposton.*?>发表于.*<span.*title="")[^<]+(?="">)"); mcTime = regTime.Matches(strPage); } string postUser = strPage.Substring(strPage.IndexOf("class=\"pi\""), 300); //获取发帖人 Regex regUser = new Regex(@"(?i)(?<=<a.*?uid=)[^<]+(?="".*?target)"); MatchCollection mcUser = regUser.Matches(postUser); if (mcUser.Count == 0) { regUser = new Regex(@"(?i)(?<=匿名.*?<em>)[^<]+(?=</em>)"); mcUser = regUser.Matches(postUser); } Sys_TaskDailyInfo model = new Sys_TaskDailyInfo(); dic["title"] = mcTitle[0].Groups[0].ToString(); if (mcUser[0].Groups[0].ToString().IndexOf('.') >= 0) { dic["user"] = "******"; } else { try { int accountid = T_AccountBLL.GetAccIdBybbsId(Convert.ToInt32(mcUser[0].Groups[0].ToString())); dic["user"] = T_AccountBLL.GetCompanyName(accountid); } catch (Exception ex) { Logger.Error("转换论坛ID出错!", ex); dic["user"] = "******"; } } return(dic); }
/// <summary> /// 插入论坛建议反馈 /// </summary> /// <param name="url"></param> /// <returns></returns> public static string GetForumFeedBack(string url, string uName, string remark) { if (url.IndexOf("www.i200.cn/bbs") < 0) { return("");//不是合法地址 } //制定编码格式 var encoding = Encoding.GetEncoding("utf-8"); //url的设置 var request = (HttpWebRequest)WebRequest.Create(url); //设置请求的方式 request.Method = "Get"; //设置Content-Type 的值 //request.ContentType = "application/x-www-form-urlencoded"; //request.ContentLength = data.Length; //var outStream = request.GetRequestStream(); //outStream.Write(data, 0, data.Length); //outStream.Close(); var response = (HttpWebResponse)request.GetResponse(); var srContent = new System.IO.StreamReader(response.GetResponseStream(), encoding); //获取抓取下来的页面内容 var strPage = srContent.ReadToEnd(); response.Close(); srContent.Close(); //获取帖子标题 Regex regTitle = new Regex(@"(?i)(?<=<a.*?id=""thread_subject"".*?>)[^<]+(?=</a>)"); MatchCollection mcTitle = regTitle.Matches(strPage); //获取帖子发表时间 Regex regTime = new Regex(@"(?i)(?<=<em.*?id=""authorposton.*?>发表于)[^<]+(?=</em>)"); MatchCollection mcTime = regTime.Matches(strPage); if (mcTime.Count == 0) { regTime = new Regex(@"(?i)(?<=<em.*?id=""authorposton.*?>发表于.*<span.*title="")[^<]+(?="">)"); mcTime = regTime.Matches(strPage); } string postUser = strPage.Substring(strPage.IndexOf("class=\"pi\""), 300); //获取发帖人 Regex regUser = new Regex(@"(?i)(?<=<a.*?uid=)[^<]+(?="".*?target)"); MatchCollection mcUser = regUser.Matches(postUser); if (mcUser.Count == 0) { regUser = new Regex(@"(?i)(?<=匿名.*?<em>)[^<]+(?=</em>)"); mcUser = regUser.Matches(postUser); } Sys_TaskDailyInfo model = new Sys_TaskDailyInfo(); try { model.t_mk = mcTitle[0].Groups[0].ToString(); model.dt_Time = Convert.ToDateTime(mcTime[0].Groups[0].ToString()); if (mcUser[0].Groups[0].ToString().IndexOf('.') >= 0) { model.accountid = 0; } else { try { model.accountid = T_AccountBLL.GetAccIdBybbsId(Convert.ToInt32(mcUser[0].Groups[0].ToString())); } catch (Exception ex) { Logger.Error("转换论坛ID出错!", ex); model.accountid = 0; } } model.inertTime = DateTime.Now; model.dt_Level = 9; model.dt_Status = 99; model.insetName = uName; model.dt_remark = "@(" + url + ")" + remark; model.dt_logUid = 0; model.dt_Source = "论坛反馈"; if (Sys_TaskDailyBLL.CheckForumUrl(url)) { return("已经存在该条反馈!"); } else { if (Sys_TaskDailyBLL.AddModel(model) > 0) { return("添加成功!"); } else { return("添加出错!"); } } } catch (Exception ex) { Logger.Error("添加论坛反馈出错!", ex); return("添加反馈出错!"); } //return mcTitle[0].Groups[0].ToString() + " " + mcTime[0].Groups[0].ToString() + " " + mcUser[0].Groups[0].ToString(); //foreach (Match m in mc) //{ // Console.WriteLine(m.Groups[0].ToString()); //} //return strPage; }