谷歌汇率转换API错误 [英] Google Currency Converter API Error
本文介绍了谷歌汇率转换API错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的ASPX code:
<%@页标题=主页LANGUAGE =C#的MasterPageFile =〜/ Site.master母AutoEventWireup =真codeFILE =默认。 aspx.cs继承=_默认%GT;< ASP:内容ID =HeaderContent=服务器ContentPlaceHolderID =HeadContent>
< / ASP:内容>
< ASP:内容ID =的BodyContent=服务器ContentPlaceHolderID =日程地址搜索Maincontent>
< ASP:标签ID =Label1的=服务器文本=从货币:前景色=黑>< / ASP:标签>
< ASP:文本框ID =TextBox1的=服务器>< / ASP:文本框>< BR />< BR />
< ASP:标签ID =Label2的=服务器文本=选择货币:前景色=黑>< / ASP:标签>
< ASP:文本框ID =TextBox2中=服务器>< / ASP:文本框>< BR />< BR />
< ASP:标签ID =LABEL3=服务器文本=金额:前景色=黑>< / ASP:标签>
< ASP:文本框ID =TextBox3=服务器>< / ASP:文本框>< BR />< BR />
< ASP:标签ID =Label4=服务器文本=速率:前景色=黑>< / ASP:标签>
< ASP:文本框ID =TextBox4=服务器>< / ASP:文本框>< BR />< BR />
< ASP:按钮的ID =Button1的=服务器文本=按钮的onclick =的button1_Click/>
< / ASP:内容>
和我的C#code:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.Net;
使用System.Text.RegularEx pressions;公共部分类_Default:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
}
保护无效的button1_Click(对象发件人,EventArgs的发送)
{
小数量= 0;
字符串fromCurrency =;
字符串toCurrency =;
fromCurrency = TextBox1.Text.ToUpper();
toCurrency = TextBox2.Text.ToUpper();
金额= Convert.ToDecimal(TextBox3.Text); Web客户端的Web =新的WebClient();
开放的我们的uri =新的URI(的String.Format(http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1},fromCurrency,toCurrency,量));
串响应= web.DownloadString(URI);
正则表达式的regex =新的正则表达式(RHS:\\\\\\。(\\\\ D * \\\\ D *));
匹配匹配= regex.Match(响应);
字符串测试= match.ToString();
小数率= Convert.ToDecimal(match.Groups [1]。价值);
TextBox4.Text = rate.ToString();
}
}
我用这code为货币转换当我点击按钮它给人的错误
输入字符串的不正确的格式,在这条线
小数率= Convert.ToDecimal(match.Groups [1] .value的);
我提交值
TextBox1的:美元
TextBox1的:INR
TextBox3:1
解决方案
下面是谷歌汇率转换使用C#的code
公共静态字符串CurrencyConvert(十进制量,串fromCurrency,串toCurrency)
{ //抓住你的价值观和构建Web请求的API
字符串apiURL =的String.Format(https://www.google.com/finance/converter?a={0}&from={1}&to={2}&meta={3}, 。量,fromCurrency,toCurrency,Guid.NewGuid()的ToString()); //使您的网络请求,并抢在结果
VAR请求= WebRequest.Create(apiURL); //获取响应
VAR的StreamReader =新的StreamReader(request.GetResponse()GetResponseStream(),System.Text.Encoding.ASCII。); //抓住你的转换值(即2.45美元)
变种结果= Regex.Matches(streamReader.ReadToEnd(),&所述;跨度类= \\BLD \\?>([^&所述] +)所述; /跨度>中)[0] .Groups [1 ]。值; //得到的结果
返回结果;
}
Here is my ASPX Code:
<%@ 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>
and my C# Code:
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();
}
}
I am using this code for Currency Conversion when i click on Button it gives the error "Input string was not in a correct format" in this line "decimal rate = Convert.ToDecimal(match.Groups[1].Value);"
I submit value in
TextBox1 : USD
TextBox1 : INR
TextBox3 : 1
解决方案
Here's the code for Google Currency Converter using c#
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;
}
这篇关于谷歌汇率转换API错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文