JavaScript EditorFreeware JavaScript Editor     Ajax Tutorials 

Main Page

Previous Page
Next Page

Cross-Domain Web Services

So far in this chapter you've been calling a service that resides on the same domain as the page accessing it. By doing this, you have avoided the problem of cross-domain scripting (also known as cross-site scripting, or XSS). As discussed earlier in this book, this problem is caused by the fact that there are security risks in allowing calls to external web sites. If the web service is on the same domain as the calling page, the browser will permit the SOAP request, but what if you want to use one of the Google or services?

For this you'll need to use a server-side proxy, which runs on your web server and makes calls on behalf of the client. The proxy then returns the information it receives back to the client. The setup resembles that of a web proxy server that is commonplace in corporate networks. In that model, all requests are passed to a central server that retrieves the web page and passes it to the requestor.

The Google Web APIs Service

Google provides a number of methods that can be called through its web service, including methods for retrieving cached copies of pages as well as the more obvious results for a given search phrase. The method you will use to demonstrate a server-side proxy is doSpellingSuggestion, which takes a phrase and returns what Google believes you meant to type. If your phrase is not misspelled or is so obscure that it cannot hazard a suggestion, an empty string is returned.


The developers' kit for the Google Web APIs service can be found at The kit contains the service's SOAP and WSDL standards, as well as examples in C#, Visual Basic .NET, and Java.

You will need to register with Google to receive a security key to allow you to access the service. There are also rules about how it can be used both commercially and for development purposes.

Setting Up the Proxy

After you have downloaded the Google documentation and received your key, you need to set up a service on your own server that will accept the calls from your users and pass them to Google. The basic service is built in the same way as the Math service discussed earlier.

To begin, open the IIS admin tool (StartAdministrative ToolsInternet Information Services). Expand the tree on the left to show the Default Web Site node, and then right-click and choose NewVirtual Directory. In the Alias field of the Virtual Directory Creation Wizard, name your new directory GoogleProxy, and then click Next. On the next screen, browse to the standard IIS directory of C:\InetPub\wwwroot, and create a new folder, also named GoogleProxy. Accept the defaults for the remaining screens of the wizard, and then use Windows Explorer to create a new folder below GoogleProxy named bin.

Next, open your text editor and create the following file, all on one line:

<%@ WebService Language=" c#"
    Codebehind=" GoogleProxy.asmx.cs" Class=" Wrox.Services.GoogleProxyService" %>

Save this as GoogleProxy.asmx in the GoogleProxy directory.

Now create the main file, GoogleProxy.asmx.cs:

using System;
using System.Web;
using System.Web.Services;
using GoogleService;

namespace Wrox.Services
  [WebService (Description = "Enables calls to the Google API",
               Namespace = "")]
  public class GoogleProxyService : System.Web.Services.WebService
    readonly string GoogleKey = "EwVqJPJQFHL4inHoIQMEP9jExTpcf/KG";

     Description = "Returns Google spelling suggestion for a given phrase.")]
  public string doSpellingSuggestion(string Phrase)
    GoogleSearchService s = new GoogleSearchService();
    s.Url = "";
    string suggestion = "";
      suggestion = s.doSpellingSuggestion(GoogleKey, Phrase);

    catch(Exception Ex)
      throw Ex;
      if (suggestion == null) suggestion = "No suggestion found.";
      return suggestion;

Remember to enter the value of your Google key for the GoogleKey variable, and then save this file to the same location as the others.

The code itself is fairly straightforward; all the real work is done by the GoogleSearchService. The method doSpellingSuggestion creates an instance of the GoogleSearchService class. The URL of the service is then set. This step is not always necessary, but we've found that it often helps to be able to change the URL of services easily. In a production environment the URL would be read from a configuration file, enabling you to move between servers easily.

The doSpellingSuggestion method is now called, passing in the Google key and the Phrase argument. This is another advantage of using a server-side proxy: you can keep sensitive information such as your key away from the client-side environment of the browser.

If an exception is thrown, it will be re-thrown and returned as a SOAP fault. If the returned suggestion is null, a suitable string is returned; otherwise, the suggestion is passed back directly.

You now create the class to interact with Google. Begin by copying the GoogleSearch.wsdl file into the GoogleProxy folder, and then open a command prompt, navigate to GoogleProxy, and run the following (you can ignore warnings about a missing schema):

WSDL /namespace:GoogleService /out:GoogleSearchService.cs GoogleSearch.wsdl

The WSDL utility reads the GoogleSearch.wsdl file and creates the source code for a class to communicate with the service. The class will reside in the GoogleService namespace, as instructed by the first parameter to WSDL. This source code needs to be turned into a DLL and to do this you need to use the C# compiler as you did before. Enter the following at the command prompt or use the batch file named MakeGoogleServiceDLL.bat from the code download:


WSDL.exe seems to be installed in a number of different places, depending on what other Microsoft components are on the machine. On machines with Visual Studio .NET installed, it is likely to be in C:\Program Files\Microsoft Visual Studio.NET 2003\SDK\v1.1\Bin, but you may need to search for it on your machine.

C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\csc.exe /r:System.dll
/r:System.Web.dll /r:System.Web.Services.dll /t:library
/out:bin\GoogleSearchService.dll GoogleSearchService.cs

As before, the /r: parameters are telling the compiler which DLLs are needed to provide support classes to the target DLL of GoogleSearchService.

The last stage is to compile the GoogleProxy class itself:

C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\csc.exe /r:System.dll
/r:System.Web.dll /r:System.Web.Services.dll /r:bin\GoogleSearchService.dll
/t:library /out:bin\GoogleProxy.dll GoogleProxy.asmx.cs

Notice that a reference is passed in for the GoogleSearchService.dll just created.

You are now ready to test the service by entering the following URL in your browser:


You should be greeted with the standard screen, as shown in Figure 6-8.

Image from book
Figure 6-8

Click the doSpellingSuggestion link to try out the method using the built-in test harness, as shown in Figure 6-9.

Image from book
Figure 6-9

When you click the Invoke button, you will see the XML returned, as shown in Figure 6-10.

Image from book
Figure 6-10

Previous Page
Next Page

Веб Технологии » SEO 

JavaScript EditorAjax Editor     Ajax Validator