Integrate with Funnelback using ASP.NET

This article provides code for a sample wrapper that can be used to integrate with Funnelback from most ASP.NET CMS implementations.

This enables the nesting of search results within a .net CMS.

The example below integrates with the Funnelback search.html endpoint and assume you will return a HTML fragment defined as a Freemarker template within Funnelback.

Code example

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

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["user-id"] != null)
                {
                    cookieValue = Request.Cookies["user-id"].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 = "user-id";
                    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 = user-id=<value>;path...
                            // our value is at 1
                            /*foreach(string comp in components) {
                                initialValue += comp + "---";
                            }*/
                            if (components.Length > 1 && components[0] == "user-id")
                            {
                                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("user-id");
                    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());
            //    }
            //}
        }
    }
}