ASP.NET AJAX and the Proxy Pattern

Posted August 21, 2009 by Quintin de Kok
Categories: Uncategorized

Tags: , , , , , , , ,


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 ( cannot access a web service using XmlHTTP on another host ( 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 ( 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(@”{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);
  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…

So we get started!

Posted August 14, 2009 by Quintin de Kok
Categories: Information, Uncategorized

Tags: , , , , ,

Let me start by giving some information about myself. My name is Quintin de Kok, a Development Director in a small development house called QC Solutions in Pretoria, South Africa focussing on development in .NET although we often take on Open Source support projects. I have a wife of a few years now and we recently added a little girl to our family.

I was listening to .NET Rocks podcast episode #467 and got a reference to a site created by Microsoft for developers during these tough economic times, Thrive. The site means to give developers the skill sets to place themselves above the rest.

I then had an look at an interesting video of a talk given by Scott Hanselman at Devscovery here… 

I agreed with most of what Scott said and I figured what the hey, let’s start a blog!

So, I’m going to make a concerted effort to blog some interesting challenges and articles for you to view, reference and use as you wish. Leave me a comment if you’d like to get in contact or check out the contact details on our site here…