Integrate with ASP.NET CMS

Managed by | Updated .

The following code wrapper example should be usable in most ASP.NET CMS implementations (such as Sharepoint, SiteCore or Umbraco) for integrating with a Funnelback search.

.NET Wrapper Code example

Please comment blocks in/out as required (for cookies and other things).

ASP.NET example wrapper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Configuration;
public partial class search : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string query = Request.Url.Query;
            if (!string.IsNullOrEmpty(query))
            {
                string url = string.Format("<SERVER_NAME>/s/search.html{0}", query);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                /// SEND COOKIE FROM BROWSER TO FB
                string cookieValue = "";
                if (Request.Cookies["JSESSIONID"] != null)
                {
                    cookieValue = Request.Cookies["JSESSIONID"].Value;
                }
                // Comment this if you don't want basket/session support
                // set cookie container and add Funnelback session cookie
                if (!string.IsNullOrEmpty(cookieValue))
                {
                    request.CookieContainer = new CookieContainer();
                    Cookie newCookie = new Cookie();
                    newCookie.Name = "JSESSIONID";
                    newCookie.Domain = "<FUNNELBACK_SERVER_DOMAIN>";
                    //newCookie.Path = "/s";
                    newCookie.Value = cookieValue;
                    request.CookieContainer.Add(newCookie);
                }
                // Use this if you have a proxy
                /*
                bool useProxy = Convert.ToBoolean(ConfigurationManager.AppSettings["useProxy"]);
                if (useProxy)
                {
                    WebProxy wp = new WebProxy("<PROXY_SERVER>:<PROXY_PORT>", true);
                    wp.Credentials = new NetworkCredential("<PROXY_USER>", "<PROXY_PASS>", "");
                    request.Proxy = wp;
                }*/
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                //
                // Get cookie from FB and send it back to browser
                //
                string initialValue = "";
                for (int i = 0; i < response.Headers.Count; i++)
                {
                    string key = response.Headers.GetKey(i).ToString();
                    if (key == "Set-Cookie")
                    {
                        string[] cookieValues = response.Headers.GetValues(i);
                        foreach (string setcookieVal in cookieValues)
                        {
                            string[] components = setcookieVal.Split(new char[] { '=', ';' });
                            // if val = JSESSIONID=<value>;path...
                            // our value is at 1
                            /*foreach(string comp in components) {
                                initialValue += comp + "---";
                            }*/
                            if (components.Length > 1 && components[0] == "JSESSIONID")
                            {
                                initialValue = components[1];
                            }
                        }
                    }
                }
                // Comment this if you don't want basket/session support
                if (!string.IsNullOrEmpty(initialValue))
                {
                    Uri cookieUri = new Uri("<DOMAIN SEARCH IS SERVED ON>");
                    var newCookie = new HttpCookie("JSESSIONID");
                    newCookie.Value = initialValue;
                    newCookie.Domain = cookieUri.Host;
                    newCookie.Path = "/";
                    Response.Cookies.Add(newCookie);
                }
                Stream resStream = response.GetResponseStream();
                StreamReader objReader = new StreamReader(resStream);
                var html = objReader.ReadToEnd();
                // >> DO SOMETHING WITH THE HTML FROM FB HERE <<
                // I.e. assign it to a text control or other element for printing in page
            }
        }
        catch (WebException ex)
        {
            throw ex;
            //if (ex.Status == WebExceptionStatus.ProtocolError)
            //{
            //    WebResponse resp = ex.Response;
            //    using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
            //    {
            //        Response.Write(sr.ReadToEnd());
            //    }
            //}
        }
    }
}
Was this artcle helpful?

Comments