Google货币转换器API错误

时间:2014-08-14 08:00:51

标签: c# asp.net api

这是我的ASPX代码:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Label ID="Label1" runat="server" Text="From Currency : " ForeColor="Black"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
    <asp:Label ID="Label2" runat="server" Text="To Currency : " ForeColor="Black"></asp:Label>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /><br />
    <asp:Label ID="Label3" runat="server" Text="Amount : " ForeColor="Black"></asp:Label>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /><br />
    <asp:Label ID="Label4" runat="server" Text="Rate : " ForeColor="Black"></asp:Label>
    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br /><br />
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</asp:Content>

和我的C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Decimal amount = 0;
        string fromCurrency = "";
        string toCurrency = "";
        fromCurrency = TextBox1.Text.ToUpper();
        toCurrency = TextBox2.Text.ToUpper();
        amount = Convert.ToDecimal(TextBox3.Text);

        WebClient web = new WebClient();
        Uri uri = new Uri(string.Format("http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1}", fromCurrency, toCurrency, amount));
        string response = web.DownloadString(uri);
        Regex regex = new Regex("rhs: \\\"(\\d*.\\d*)");
        Match match = regex.Match(response);
        string test = match.ToString();
        decimal rate = Convert.ToDecimal(match.Groups[1].Value);
        TextBox4.Text = rate.ToString();
    }
}

当我点击按钮时,我使用此代码进行货币转换它会给出错误 &#34;输入字符串的格式不正确&#34;在这一行 &#34;十进制率= Convert.ToDecimal(match.Groups [1] .Value);&#34;

我在

中提交了价值
TextBox1 : USD
TextBox1 : INR
TextBox3 : 1

1 个答案:

答案 0 :(得分:5)

以下是使用c#

的Google货币转换器代码
 public static string CurrencyConvert(decimal amount, string fromCurrency, string toCurrency)
    {

        //Grab your values and build your Web Request to the API
        string apiURL = String.Format("https://www.google.com/finance/converter?a={0}&from={1}&to={2}&meta={3}", amount, fromCurrency, toCurrency, Guid.NewGuid().ToString());

        //Make your Web Request and grab the results
        var request = WebRequest.Create(apiURL);

        //Get the Response
        var streamReader = new StreamReader(request.GetResponse().GetResponseStream(), System.Text.Encoding.ASCII);

        //Grab your converted value (ie 2.45 USD)
        var result = Regex.Matches(streamReader.ReadToEnd(), "<span class=\"?bld\"?>([^<]+)</span>")[0].Groups[1].Value;

        //Get the Result
        return result;
    }