Posted tagged ‘Proxy’

ASP.NET AJAX and the Proxy Pattern

August 21, 2009

Introduction

As promised, the first article for my blog has arrived. I was asked by a client to add spell checker functionality to line of business application written in C# ASP.NET and AJAX. I went on a Google rampage trying to find a free spell checker that would do the job properly. There are tons of spell checker implementations for ASP.NET out there developed by some of the leading dev companies. The problem is I couldn’t find a free implementation that looked good and worked well. So I decided that I needed to find a pure JScript implementation of a spell checker and use that instead.

After searching a while I got the Orangoo AJAX implementation of a Google like spell checker that uses the Google Toolbar Spell Check API that was reverse engineered from the Google toolbar; a hack I know but; why not!

I was able to get the demo of the implementation working immediately after download, open the demo_single.html file and viola, it works. When I ported it to ASP.NET I was hit by a curve ball called the “same domain policy”. In short; JavaScript from one host (http://yourhost.com) cannot access a web service using XmlHTTP on another host (http://otherhost.com) due to security concerns etc. You have basically got 2 options that you can use, JSONP (which is way out of my league) or you can implement a proxy pattern, also known as (Mash It Up), where a web service is called on your domain (http://yourhost.com/service) and it, in turn, accesses the remote host and returns the data to your page. The proxy pattern has many advantages but ease of implementation remains the greatest.

To implement a XmlHTTP proxy you can either create an elaborate web service that takes the requested information and passed it on with security etc. or you create a very simple page that proxy’s for you, assuming that you’ve used Windows or Forms authentication to reach the page as a security measure.

Then it’s as simple as adding the following to your page load event:

  1.     protected void Page_Load()
  2.     {
  3.         // create the web request to the remote server
  4.         System.Net.WebRequest webRequest =
  5.             System.Net.HttpWebRequest.Create(
  6.                 string.Format(@”https://www.google.com/tbproxy/spell?lang={0}”,
  7.                     (this.Request.QueryString[“lang”] == “” ? “en” : this.Request.QueryString[“lang”])));
  8.         // populate request with the method (GET/POST) and content type
  9.         webRequest.Method = this.Request.HttpMethod;
  10.         webRequest.ContentType = this.Request.ContentType;
  11.         if (this.Request.HttpMethod == “POST”)
  12.         {
  13.             // populate request with input steam in case of POST
  14.             using (System.IO.StreamReader requestStream = new System.IO.StreamReader(this.Request.InputStream))
  15.             {
  16.                 System.IO.StreamWriter requestWriter = new System.IO.StreamWriter(webRequest.GetRequestStream());
  17.                 requestWriter.Write(requestStream.ReadToEnd());
  18.                 requestWriter.Flush();
  19.                 requestWriter.Close();
  20.                 requestStream.Close();
  21.             }
  22.         }
  23.         // get the reponse from the remote server
  24.         System.Net.WebResponse webResponse = webRequest.GetResponse();
  25.         // set the encoding for the response
  26.         Encoding encode = System.Text.Encoding.GetEncoding(“utf-8”);
  27.         // create the Stream Reader
  28.         System.IO.StreamReader responseStream = new System.IO.StreamReader(webResponse.GetResponseStream(), encode);
  29.         
  30.         // write xml response
  31.         this.Response.ContentType = webResponse.ContentType;
  32.         this.Response.Write(responseStream.ReadToEnd());
  33.     \

That’s that, you’ve successfully created an XmlHTTP proxy for a google spell checker that could be used for any Mash It Up scenario. Please see my disclaimer here…

Advertisements