The Currency Converter Proxy

This example shows how to take advantage of a currency conversion service, one of several free services found at www.xmethods.net/ve2/index.po.

The basic signature of the service is as follows:

float rate getRate(string countryFrom, string countryTo)

It accepts the ISO standard country abbreviation (as found in a URL) for two countries, and returns the conversion rate between their currencies. For example, calling getRate() with us and uk might return 0.57.

There are three steps to this example:

1. Create a web service with a reference to the currency conversion service.

2. Implement a method that accepts the two country strings, passes them to the service, and returns the result.

3. Create a client to take advantage of the service.

Creating the Web Service

Follow these steps to create the web service:

1. Open Visual Studio 2005 and choose New O Web Site from the File menu.

2. Pick an ASP.NET web service as the project and choose HTTP as the location.

3. Call the project CurrencyProxyService. The full URL is http://localhost/CurrencyProxyService. Click OK to create the project.

4. Right-click the project in Solution Explorer and choose Add Web Reference.

5. Enter the following URL, found on the webserviceX.NET website, into the URL box: http://www.webservicex.net/CurrencyConvertor.asmx. Click Go.

6. You should see the ConversionRate method listed. Rename the Web Reference Name to webservicex and click Add Reference, as shown in Figure 16-5.

Figure 16-5

Implementing the GetRate() Method

Open service.cs in the App_Code folder file and modify it as shown:

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

[WebService(Namespace = "http://www.wrox.com/webservices/CurrencyProxyService")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService {

[WebMethod(Description = "Accepts two currency codes and returns the currency exchange rate as a double.")]

public double GetRate(string currencyFrom, string currencyTo) {

webservicex.CurrencyConvertor exchange = new webservicex.CurrencyConvertor(); webservicex.Currency from = (webservicex.Currency)Enum.Parse(typeof(webservicex.Currency), currencyFrom); webservicex.Currency to = (webservicex.Currency)Enum.Parse(typeof(webservicex.Currency), currencyTo); double rate = exchange.ConversionRate(from, to); return rate;

As stated, the code is very short. Aside from the standard namespaces needed for the service, it has one method, GetRate().

The method creates a new instance of webservicex.CurrencyConvertor and calls its ConversionRate() method. The slightly fiddly part is caused by the fact that the service uses an enumeration for the currency codes, so the two strings need to use the static Enum.Parse() method along with a cast to convert them. It then returns the rate to the caller.

Creating the Client

Create a new HTML file named currencyProxyServiceDemo.html and save it in the CurrencyProxyService folder created earlier. Add the following HTML for the test form:

<title>Currency Service Proxy Demo</title> <!—script to come -->

Currency from:&nbsp; <input type="text" id="txtCurrencyFrom" value="USD" size="4"><br>

Currency to:&nbsp; <input type="text" id="txtCurrencyTo" value="GBP" size="4"><br>

Amount from:&nbsp;<input type="text" id="txtAmountFrom" value="1" size="10"><br> Amount to:&nbsp;<input type="text" id="txtAmountTo" size="10" readonly><br> <input type="button" value="Convert" onclick=

"getRate(document.getElementById('txtCurrencyFrom').value, document.getElementById('txtCurrencyTo').value);"> </body> </html>

Now add the following script: <head>

<title>Currency Service Proxy Demo</title>

<script type="text/javascript" src="zXml.src.js"></script>

<script type="text/javascript"> var oHttpReq = null;

function getRate(from, to) {

var sServiceUrl = "Service.asmx/GetRate";

var sQuery = "?currencyFrom=" + from + "&currencyyTo=" + to; var sUrl = sServiceUrl + sQuery; oHttpReq = zXmlHttp.createRequest();

oHttpReq.onreadystatechange = handleReadyStateChange; oHttpReq.open("GET", sUrl, true); oHttpReq.send(null);

function handleReadyStateChange() {

if (oHttpReq.status == 200 && oHttpReq.responseXML) {

showResults(oHttpReq.responseXML.documentElement.firstChild.nodeValue);

throw new Error("Unable to retrieve rate.");

function showResults(rate) {

var dAmount = document.getElementById("txtAmountFrom").value; document.getElementById("txtAmountTo").value = dAmount * rate;

This page uses the zXml library so that needs to be in the virtual folder. The first script block adds the zXml library so that this client works on both Internet Explorer and Mozilla browsers. When the user clicks the Convert button, the two values from txtCurrencyFrom and txtCurrencyTo are passed to the getRate() method:

function getRate(from, to) {

var sServiceUrl = "Service.asmx/GetRate";

var sQuery = "?currencyFrom=" + from + "&currencyTo=" + to; var sUrl = sServiceUrl + sQuery; oHttpReq = zXmlHttp.createRequest();

oHttpReq.onreadystatechange = handleReadyStateChange; oHttpReq.open("GET", sUrl, true); oHttpReq.send(null);

The method builds the full URL to your web service, including the query string, and opens a connection to the service. The handleReadyStateChange() function is assigned to be called to deal with the response:

function handleReadyStateChange() {

if (oHttpReq.status == 200 && oHttpReq.responseXML) {

showResults(oHttpReq.responseXML.documentElement.firstChild.nodeValue);

throw new Error("Unable to retrieve rate.");

When the readyState is equal to 4, the status is checked to see if it equals 200, which means the server received the request and responded correctly. A code of 404, for example, would mean that the resource could not be found. The response actually arriving looks like this:

<double xmlns="http://www.wrox.com/..... ">0.5263</double>

Therefore, to read the rate, simply take the value of the document element's first child.

There is one final step to take before testing the page. By default, ASP.NET allows only SOAP requests to a web service; GET and POST requests are forbidden. Because this service uses GET and passes the two currencies in the query string, a new section must added to the web.config file for the service. Add the following code to the <system.web> element:

<webServices> <protocols>

<!-- <add name="HttpPost"/> --> <add name="HttpGet"/> </protocols> </webServices>

http://localhost/CurrencyProxyService/currencyProxyServiceDemo.htmlto see the whole page in action, as shown in Figure 16-6."/>
Navigate to http://localhost/CurrencyProxyService/currencyProxyServiceDemo.htmlto see the whole page in action, as shown in Figure 16-6.

Figure 16-6

Figure 16-6

0 0

Post a comment

  • Receive news updates via email from this site