SharePoint2010联合搜索——Google、百度

以下图是效果图:右边添加两个Web联合结果部件,分别连接百度和Google搜索。

OpenSearchBase类----抽象类父类 

public abstract class OpenSearchBase : LayoutsPageBase
    {
        //搜索单词参数 
        public string SearchTerm
        {
            get
            {
                //根据网站的编码不同而获取的也不同
                if (Request.Cookies.Get("encoding").Value == "gb2312")
                {
                    string query = Page.Request["q"];
                    query = UrlEncode(query);
                    return query;
                }
                else
                {
                    return Page.Request["q"];
                }
            }
        }

        //Url转码
        public static string UrlEncode(string input)
        {
            if (input == null || "".Equals(input)) return "";
            StringBuilder sb = new StringBuilder();
            byte[] byStr = System.Text.Encoding.Default.GetBytes(input);
            for (int i = 0; i < byStr.Length; i++)
            {
                sb.Append(@"%" + Convert.ToString(byStr[i], 16));
            }
            return (sb.ToString());
        }

        //显示搜索条数参数
        public int Count
        {
            get
            {
                return Convert.ToInt32(Page.Request["count"]);
            }
        }

        //获取搜索结果列表XML格式
        public string GetResultsXML(string searchURL, string searchTerm, string selectNodes)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(searchURL);
            CookieContainer cookie = new CookieContainer();
            request.CookieContainer = cookie;
            WebResponse response = request.GetResponse();
            HtmlDocument doc = new HtmlDocument();
            try
            {
                if (response.ContentType == null || response.ContentType.ToLower().Contains("utf-8") || response.ContentType.ToLower().Contains("big5"))
                {
                    //Google
                    HtmlWeb hw = new HtmlWeb();
                    doc = hw.Load(searchURL);
                }
                else if (response.ContentType.ToLower().Contains("gb2312") || response.ContentType.ToLower().Contains("gbk"))
                {
                    //Baidu
                    request.Method = "GET";
                    Stream rss = response.GetResponseStream();
                    doc.Load(rss);
                }
                //Begin writing the RSS document
                StringBuilder resultsXML = new StringBuilder();
                resultsXML.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                resultsXML.Append("<rss version=\"2.0\">");
                resultsXML.AppendFormat("<channel><title></title><link/><description/><ttl>60</ttl>", searchTerm);

                HtmlNodeCollection nodeCollection = doc.DocumentNode.SelectNodes(selectNodes);
                foreach (HtmlNode htmlNode in nodeCollection)
                {
                    string link = "";
                    string title = "";
                    string desc = "";
                    StringWriter descWriter = new StringWriter();
                    StringWriter titleWriter = new StringWriter();
                    StringWriter linkWriter = new StringWriter();

                    foreach (HtmlNode subNode in htmlNode.ChildNodes)
                    {
                        //在XML内容中找到标题(title),URL地址(link),描述(desc)和节点(subNode)
                        string[] ltd = FindResultItems(link, title, desc, subNode);
                        link = ltd[0];
                        title = ltd[1];
                        desc = ltd[2];
                    }
                    Server.HtmlEncode(desc, descWriter);
                    string encDescription = descWriter.ToString();
                    if (encDescription == "")
                    {
                        encDescription = "不能正常显示信息,如图片等";
                    }
                    Server.HtmlEncode(title, titleWriter);
                    string encTitle = titleWriter.ToString();
                    Server.HtmlEncode(link, linkWriter);
                    string encLink = linkWriter.ToString();

                    resultsXML.AppendFormat("<item><title>{0}</title><link>{1}</link><description>{2}</description><pubDate>2010-10-10</pubDate></item>", encTitle, encLink, encDescription);

                }

                //Complete RSS document
                resultsXML.Append("</channel></rss>");
                return resultsXML.ToString();
            }
            catch (Exception ex1)
            {
                StringBuilder errorXML = new StringBuilder();
                errorXML.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                errorXML.Append("<rss version=\"2.0\">");
                //errorXML.Append("<channel><title>没有合适的结果</title><link/><description/><pubDate/>");
                errorXML.Append("<channel><title>错误信息</title><link/><description/><pubDate/>");
                errorXML.AppendFormat("<item><title>错误信息</title><link/><description>{0}</description><pubDate/></item>", ex1.ToString());
                errorXML.Append("</channel></rss>");
                return errorXML.ToString();
            }
            finally
            {
                response.Close();
            }
        }

        //在XML内容中找到标题(title),URL地址(link),描述(desc)和节点(subNode)
        public abstract string[] FindResultItems(string link, string title, string desc, HtmlNode subNode);
    }

百度BaiduSearch类

public partial class BaiduSearch : OpenSearchBase
    {
        protected override void Render(HtmlTextWriter writer)
        {
            //创建Cookie,因百度网站的编码是GB2312,设Cookie为gb2312
            HttpCookie cookie = new HttpCookie("encoding", "gb2312");
            Request.Cookies.Add(cookie);
            //获取搜索词
            string searchTerm = base.SearchTerm;
            if (string.IsNullOrEmpty(searchTerm)) return;
            //获取搜索个数
            int count = base.Count;
            //构建URL地址
            string searchURL = string.Format("http://www.baidu.com/s?wd={0}&rn={1}", searchTerm, count);
            Response.ContentType = "text/xml";
            //用HTMLTextWriter对象写出RSS文档
            writer.Write(base.GetResultsXML(searchURL, searchTerm, "//td[@class='f']"));
        }

        //重写FindResultItems方法
        public override string[] FindResultItems(string link, string title, string desc, HtmlNode subNode)
        {
            string[] ltd = new string[3];
            ltd[0] = link;
            ltd[1] = title;
            ltd[2] = desc;
            if (subNode.Name == "a")
            {
                ltd[0] = subNode.Attributes["href"].Value;
                ltd[1] = subNode.InnerText.ToString();
            }
            if (subNode.Name == "font")
            {
                string descHtml = subNode.InnerHtml;

                try
                {
                    ltd[2] = descHtml.Substring(0, descHtml.IndexOf("<br>"));
                }
                catch
                {
                    ltd[2] = "不能正常显示...比如含有图片...";
                }
            }
            return ltd;
        }
    }

谷歌GoogleSearch类

public partial class GoogleSearch : OpenSearchBase
    {
        protected override void Render(HtmlTextWriter writer)
        {
            //设置Cookie为utf-8
            HttpCookie cookie = new HttpCookie("encoding", "utf-8");
            Request.Cookies.Add(cookie);
            //获取搜索词和搜索个数,并创建搜索地址URL
            string searchTerm = base.SearchTerm;
            if (string.IsNullOrEmpty(searchTerm)) return;
            int count = base.Count;
            string searchURL = string.Format("http://www.google.com.hk/search?hl=zh-cn&q={0}", searchTerm);
            if (count != 0)
            {
                searchURL = searchURL + string.Format("&num={0}", count);
            }
            Response.ContentType = "text/xml";
            //用HTMLTextWriter对象写出RSS文档
            writer.Write(base.GetResultsXML(searchURL, searchTerm, "//li[@class='g']"));
        }

        //重写FindResultItems方法
        public override string[] FindResultItems(string link, string title, string desc, HtmlNode subNode)
        {
            string[] ltd = new string[3];
            ltd[0] = link;
            ltd[1] = title;
            ltd[2] = desc;
            //Find the list that contains the result items
            if (subNode.Name == "h3")
            {
                foreach (HtmlNode lineItemNode in subNode.ChildNodes)
                {
                    ltd[0] = lineItemNode.Attributes[0].Value;
                    ltd[1] = lineItemNode.InnerText.ToString();
                }
            }
            if (subNode.Name == "div")
            {
                string descHtml = subNode.InnerHtml;
                try
                {
                    ltd[2] = descHtml.Substring(0, descHtml.IndexOf("<br>"));
                }
                catch
                {
                    ltd[2] = "有图片,不能显示。。。";
                }
            }
            return ltd;
        }
    }

 

posted on 2010-12-20 11:28  Alex Jin  阅读(719)  评论(0编辑  收藏  举报

导航