This article is written for an old version of the Virtual Earth platform. While still available for reference purposes, it is unlikely to work if implemented.
As outlined in previous articles, in order to use Virtual Earth on a commercial website Microsoft has provided a special "commercialized" version of the MapControl. The idea behind this control is that Microsoft allows you to use the control for free but reserves the right to place advertising on the control. The advertising will come in the form of geographically relevant content based on the searches carried out by the user.
In order to get the searches working on the Virtual Earth commercial control you will need to implement two pages on your server that act as proxies to the actual searches on the MSN site. The two searches are for are the ads (what) and the location (where).
We've written a version of these proxies taking advantage of the generic HTTP handler (ASHX) feature of ASP.NET 2.0. What.ashx and Where.ashx source code below:
<%@ WebHandler Language="C#" Class="What" %>
using System;
using System.IO;
using System.Web;
using System.Net;
using System.Text;
public class What : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
if (context.Request.QueryString.Count > 0)
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://virtualearth.msn.com/ads.ashx");
myHttpWebRequest.Method = "POST";
myHttpWebRequest.ServicePoint.Expect100Continue = false;
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
UTF8Encoding myUTF8Encoding = new UTF8Encoding();
byte[] bytes = myUTF8Encoding.GetBytes(String.Format("a={0}&b={1}&c={2}&d={3}", context.Request["a"], context.Request["b"], context.Request["c"], context.Request["d"]));
myHttpWebRequest.ContentLength = bytes.Length;
Stream myRequestStream = myHttpWebRequest.GetRequestStream();
myRequestStream.Write(bytes, 0, bytes.Length);
myRequestStream.Close();
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream myResponseStream = myHttpWebResponse.GetResponseStream();
Encoding myEncoding = Encoding.GetEncoding("utf-8");
StreamReader myStreamReader = new StreamReader(myResponseStream, myEncoding);
// Read 256 characters at a time
StringBuilder myStringBuilder = new StringBuilder();
Char[] buffer = new Char[256];
int count = myStreamReader.Read(buffer, 0, 256);
while (count > 0)
{
myStringBuilder.Append(new String(buffer, 0, count));
count = myStreamReader.Read(buffer, 0, 256);
}
myHttpWebResponse.Close();
context.Response.ContentType = "text/html";
context.Response.Write(myStringBuilder.ToString());
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
<%@ WebHandler Language="C#" Class="Where" %>
using System;
using System.IO;
using System.Web;
using System.Net;
using System.Text;
public class Where : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
if (context.Request.QueryString.Count > 0)
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://virtualearth.msn.com/search.ashx");
myHttpWebRequest.Method = "POST";
myHttpWebRequest.ServicePoint.Expect100Continue = false;
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
UTF8Encoding myUTF8Encoding = new UTF8Encoding();
byte[] bytes = myUTF8Encoding.GetBytes(String.Format("a={0}&b={1}&c={2}&d={3}&e={4}&f={5}&g={6}&i={7}&r={8}", context.Request["a"], context.Request["b"], context.Request["c"], context.Request["d"], context.Request["e"], context.Request["f"], context.Request["g"], context.Request["i"], context.Request["r"]));
myHttpWebRequest.ContentLength = bytes.Length;
Stream myRequestStream = myHttpWebRequest.GetRequestStream();
myRequestStream.Write(bytes, 0, bytes.Length);
myRequestStream.Close();
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream myResponseStream = myHttpWebResponse.GetResponseStream();
Encoding myEncoding = Encoding.GetEncoding("utf-8");
StreamReader myStreamReader = new StreamReader(myResponseStream, myEncoding);
// Read 256 characters at a time
StringBuilder myStringBuilder = new StringBuilder();
Char[] buffer = new Char[256];
int count = myStreamReader.Read(buffer, 0, 256);
while (count > 0)
{
myStringBuilder.Append(new String(buffer, 0, count));
count = myStreamReader.Read(buffer, 0, 256);
}
myHttpWebResponse.Close();
context.Response.ContentType = "text/html";
context.Response.Write(myStringBuilder.ToString());
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
Article contributed by Shawn Miller. Have you got something to contribute?