Hämta aktuella växelkurser med Web Service

Uppdatering: Web servicen som används i detta exemplet verkar inte finnas kvar längre på nätet.

Inspirerad av en artikel om växelkurser på Code Project har jag gjort ett eget exempel. Jag använder en web service som finns xmethods.net.

Registrera tjänsten
Först måste man använda Visual Studio till att lägga till en referens till servicen.

1. Högerklicka på rooten i Solution Explorern
2. Välj "Add Web Reference"
3. Skriv in url till tjänsten. I vårt fall: http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl
4. Skriv in "Web Reference Name". Vi döper den till Rate_WS
5. Då skapas en ny mapp som heter "App_WebReferences" i projektet.

När du har gjort ovanstående så kan man använda tjänsten från sin kod. Här följer ett enkelt exempel som skriver ut några växelkurser.

<%@ Page Language="C#" Trace="true" validateRequest="false" EnableSessionState="True" EnableViewState="true" SmartNavigation="false"%>
<script Language="c#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
try
{
Rate_WS.CurrencyExchangeService to_currency = new Rate_WS.CurrencyExchangeService();
float Euro_Dollar = to_currency.getRate("euro", "united states");
float Dollar_Sek = to_currency.getRate("united states", "sweden");
float Euro_Sek = to_currency.getRate("euro", "sweden");
float Sek_Yen = to_currency.getRate("sweden", "japan");

lblEuro_Sek.Text = Euro_Sek.ToString();
lblDollar_Sek.Text = Dollar_Sek.ToString();
lblEuro_Dollar.Text = Euro_Dollar.ToString();
lblSek_Yen.Text = Sek_Yen.ToString();
}
catch (Exception) { }
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Currency converter in ASP.NET</title>
<link rel="stylesheet" href="../example.css" type="text/css" />

</head>

<body>
<div id="content">
<b>Currency example using web service</b>
<br /><br />

1 US Dollar = <asp:Label ID="lblDollar_Sek" runat="server" /> SEK
<br /><br />
1 Euro = <asp:Label ID="lblEuro_Sek" runat="server" /> SEK
<br /><br />
1 Euro = <asp:Label ID="lblEuro_Dollar" runat="server" /> US Dollar
<br /><br />
1 SEK = <asp:Label ID="lblSek_Yen" runat="server" /> Yen

</div>
</body>
</html>

Länk till sida som visar kurser i real-tid (Exemplet är nedtaget eftersom web servicen inte längre finns på nätet)

Mer läsning:
Läs även den danska bloggen Walk the walk som i ett exempel konsumerar en tjänst på Webservicex.net. Den verkar bra eftersom man anger valutabetäckningen (SEK, USD, DKK osv) som inparameter.

Comments

[…] Nu tänkte jag prova en annan tjänst från Webservicex.net. Exemplet hittade jag i den danska bloggen Walk the walk. […]

thanks for sharing a useful information. http://www.onlinepromotionuae.ae

thanks for sharing McAfee Help Number

Very Very nice post. its a very useful and outstanding article. Thanks for that. norton tech support number

Hii, Thanks for the sharing nice posting with us. i’m really impressed.Acer Laptop Help

We are really grateful for your blog. There are many approaches to visit the post office. Good work. hotmail help

Very interesting Article. The way you have shown everything is simply great. Thank you so much for entertaining us for so long.

Acer Laptop Support Number UK Acer Laptop Support

Outstanding Post.

hotmail support

hotmail support number

Hii, Thanks for the sharing nice posting with us. i’m really impressed.tech support for routers issues...

very very nice post. thanks for the sharing with us.

software solutions uk

this is outstanding and great post!

software support

Thank you so much for sharing this with us. The way, you describe everything is just Awsum. I am pleased with you.

microsoft support microsoft helpline windows customer service

Thanks for sharing this informative blog. This blog is very useful for evryone. keep it on.

lexmark helpline number lexmark customer service phone number lexmark customer support phone number

Lovely website! I am loving it!! Will come back again. I am taking your feeds also. HP customer service HP support number HP printer technical support

I'm very happy to see this web because the info I'm looking for at this time is in here. Dell printer support Dell printer support number

Pretty good post. Epson Support Number

Please fill out all the fields.

*
*