C# 如何从URL获取值/变量?
因此,我想写一个BTC转换器应用程序,我可以在 将URL中的GBP改为USD会自然而然地将其改为USD,我想使用它将数据解析为一个变量,然后将其作为一个普通变量使用。但是我希望用户能够输入他们的货币并更改url,然后以一加元的价格获取amount。如何将GBP用作变量,然后根据用户输入进行更改。 我在想一个最流行的货币下拉框,但我根本不知道如何使用它C# 如何从URL获取值/变量?,c#,bitcoin,C#,Bitcoin,因此,我想写一个BTC转换器应用程序,我可以在 将URL中的GBP改为USD会自然而然地将其改为USD,我想使用它将数据解析为一个变量,然后将其作为一个普通变量使用。但是我希望用户能够输入他们的货币并更改url,然后以一加元的价格获取amount。如何将GBP用作变量,然后根据用户输入进行更改。 我在想一个最流行的货币下拉框,但我根本不知道如何使用它 好心点,我是一个noob,正在尝试制作我的第一个有用的应用程序。下面是一个简单的示例,说明如何获得不同货币的价值: using System; u
好心点,我是一个noob,正在尝试制作我的第一个有用的应用程序。下面是一个简单的示例,说明如何获得不同货币的价值:
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace ConsoleApp5
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetValueAsync("GBP").Result);
Console.WriteLine(GetValueAsync("USD").Result);
Console.WriteLine(GetValueAsync("RUB").Result);
}
public static async Task<string> GetValueAsync(string curr)
{
using (HttpClient client = new HttpClient())
{
var responseString = await client.GetStringAsync("https://blockchain.info/tobtc?currency="+curr+"&value=1");
return responseString;
}
}
}
}
正在通过提供的URL发送异步http get请求,并以字符串形式返回响应。您要使用的站点正在以字符串形式返回值,这就是为什么它可以工作的原因。
由于请求是异步的,我们必须使用
wait
,以便获得字符串形式的响应
如果您想在WinForm中执行此操作。下面是一个例子。假设您已经为输入值设置了文本框
,为显示结果设置了标签
,为获取结果设置了按钮
。它们可以通过从工具箱中下拉添加到表单中
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void button1_ClickAsync(object sender, EventArgs e)
{
string curr = textBox1.Text;
if (!string.IsNullOrEmpty(curr))
{
label2.Text = "waiting for response";
var res = await GetValueAsync(curr);
label2.Text = res;
}
}
public async Task<string> GetValueAsync(string curr)
{
var responseString = string.Empty;
using (HttpClient client = new HttpClient())
{
string reqString = "https://blockchain.info/tobtc?currency=" + curr + "&value=1";
responseString = await client.GetStringAsync(reqString);
}
return responseString;
}
}
}
使用系统;
使用System.Net.Http;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间WindowsFormsApp3
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有异步无效按钮1\u ClickAsync(对象发送方,事件参数e)
{
字符串curr=textBox1.Text;
如果(!string.IsNullOrEmpty(curr))
{
label2.Text=“等待响应”;
var res=等待GetValueAsync(curr);
label2.Text=res;
}
}
公共异步任务GetValueAsync(字符串curr)
{
var responseString=string.Empty;
使用(HttpClient=new HttpClient())
{
字符串请求字符串=”https://blockchain.info/tobtc?currency=“+curr+”&value=1”;
responseString=await client.GetStringAsync(reqString);
}
回报率;
}
}
}
这是Win表单的完整解决方案
以下是对您有用的链接:
以下是如何进行此操作的录音:
以下是一个简单的示例,说明如何获取不同货币的价值:
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace ConsoleApp5
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetValueAsync("GBP").Result);
Console.WriteLine(GetValueAsync("USD").Result);
Console.WriteLine(GetValueAsync("RUB").Result);
}
public static async Task<string> GetValueAsync(string curr)
{
using (HttpClient client = new HttpClient())
{
var responseString = await client.GetStringAsync("https://blockchain.info/tobtc?currency="+curr+"&value=1");
return responseString;
}
}
}
}
正在通过提供的URL发送异步http get请求,并以字符串形式返回响应。您要使用的站点正在以字符串形式返回值,这就是为什么它可以工作的原因。
由于请求是异步的,我们必须使用
wait
,以便获得字符串形式的响应
如果您想在WinForm中执行此操作。下面是一个例子。假设您已经为输入值设置了文本框
,为显示结果设置了标签
,为获取结果设置了按钮
。它们可以通过从工具箱中下拉添加到表单中
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void button1_ClickAsync(object sender, EventArgs e)
{
string curr = textBox1.Text;
if (!string.IsNullOrEmpty(curr))
{
label2.Text = "waiting for response";
var res = await GetValueAsync(curr);
label2.Text = res;
}
}
public async Task<string> GetValueAsync(string curr)
{
var responseString = string.Empty;
using (HttpClient client = new HttpClient())
{
string reqString = "https://blockchain.info/tobtc?currency=" + curr + "&value=1";
responseString = await client.GetStringAsync(reqString);
}
return responseString;
}
}
}
使用系统;
使用System.Net.Http;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间WindowsFormsApp3
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有异步无效按钮1\u ClickAsync(对象发送方,事件参数e)
{
字符串curr=textBox1.Text;
如果(!string.IsNullOrEmpty(curr))
{
label2.Text=“等待响应”;
var res=等待GetValueAsync(curr);
label2.Text=res;
}
}
公共异步任务GetValueAsync(字符串curr)
{
var responseString=string.Empty;
使用(HttpClient=new HttpClient())
{
字符串请求字符串=”https://blockchain.info/tobtc?currency=“+curr+”&value=1”;
responseString=await client.GetStringAsync(reqString);
}
回报率;
}
}
}
这是Win表单的完整解决方案
以下是对您有用的链接:
以下是如何进行此操作的录音:
还在谷歌上搜索,今天睡得不好,真的很慢,但我需要做一个适当的应用程序。你是在问如何从用户那里获取值,如何使用用户提供的数据构造url,还是如何解析得到的响应?您的问题需要更具体地说明您正在努力解决的问题的哪一部分,以避免我们试图解决您实际没有遇到的问题(并且因为在堆栈溢出问题上,我们更喜欢聚焦紧密的问题,而不是过于宽泛的问题(事实上,问题可以因为过于宽泛而关闭)。仍然在谷歌上搜索,今天睡得不好,速度真的很慢,但我需要做一个适当的应用程序。并且需要尽可能多地练习。你是在问如何从用户那里获取值,如何使用用户提供的数据构建url,还是如何解析你得到的响应?你的问题需要更具体地说明到底是哪一部分您正在努力解决的问题,以避免我们试图解决您实际上没有的问题(并且因为在堆栈溢出上,我们更喜欢紧密关注的问题,而不是过于宽泛的问题(事实上,问题可以因为过于宽泛而关闭)。遗憾的是,我不知道如何实现它并在我将查找有关Async的表单中使用它。我强烈反对将值连接到字符串中,而不是像
GetStringAsync($)https://....currency={curr}&value=1“;
将better@Ortund没有区别,,