C# 在按钮上单击 API 值到标签
C# on button click API value to Lable
我正在尝试检查电子邮件是否 valid/invalid/unknown 使用 GMASS API 发送“状态”作为字符串值。
例如尝试这些链接:
有效的电子邮件回复URL
无效的电子邮件回复URL
从 JSON 中检索“状态”值并在单击按钮时在标签中设置,如何实现?
我试过的,里面的按钮这段代码:
textBox.Text = "random@gmail.com";
HttpClient client1 = new HttpClient();
async Task Main1(string[] args)
{
string api = "https://verify.gmass.co/verify?email="+ textBox.Text + "&key=52D5D6DD-CD2B-4E5A-A76A-1667AEA3A6FC";
string response = await client1.GetStringAsync(api);
var status = JsonConvert.DeserializeObject<dynamic>(response);
label.Text = status.Status;
}
我的代码出了什么问题?
在控制台应用程序中对其进行了测试,能够获得状态 {invalid}
using (var client = new HttpClient())
{
string api = "https://verify.gmass.co/verify?email=" + textBox.Text + "&key=52D5D6DD-CD2B-4E5A-A76A-1667AEA3A6FC";
string response = client.GetStringAsync(api).Result;
var status = JsonConvert.DeserializeObject<dynamic>(response);
var x = status.Status;
}
对于异步任务你可以这样做
注意:获取结果和更新标签需要一点时间
private async void button1_Click(object sender, EventArgs e)
{
label1.Text = await GetStatus(textBox1.Text);
}
public static async Task<string> GetStatus(string email)
{
using (var client = new HttpClient())
{
string api = "https://verify.gmass.co/verify?email=" + email + "&key=52D5D6DD-CD2B-4E5A-A76A-1667AEA3A6FC";
string response = await client.GetStringAsync(api);
var status = JsonConvert.DeserializeObject<dynamic>(response);
return status.Status;
}
}
我正在尝试检查电子邮件是否 valid/invalid/unknown 使用 GMASS API 发送“状态”作为字符串值。
例如尝试这些链接:
有效的电子邮件回复URL
无效的电子邮件回复URL
从 JSON 中检索“状态”值并在单击按钮时在标签中设置,如何实现?
我试过的,里面的按钮这段代码:
textBox.Text = "random@gmail.com";
HttpClient client1 = new HttpClient();
async Task Main1(string[] args)
{
string api = "https://verify.gmass.co/verify?email="+ textBox.Text + "&key=52D5D6DD-CD2B-4E5A-A76A-1667AEA3A6FC";
string response = await client1.GetStringAsync(api);
var status = JsonConvert.DeserializeObject<dynamic>(response);
label.Text = status.Status;
}
我的代码出了什么问题?
在控制台应用程序中对其进行了测试,能够获得状态 {invalid}
using (var client = new HttpClient())
{
string api = "https://verify.gmass.co/verify?email=" + textBox.Text + "&key=52D5D6DD-CD2B-4E5A-A76A-1667AEA3A6FC";
string response = client.GetStringAsync(api).Result;
var status = JsonConvert.DeserializeObject<dynamic>(response);
var x = status.Status;
}
对于异步任务你可以这样做
注意:获取结果和更新标签需要一点时间
private async void button1_Click(object sender, EventArgs e)
{
label1.Text = await GetStatus(textBox1.Text);
}
public static async Task<string> GetStatus(string email)
{
using (var client = new HttpClient())
{
string api = "https://verify.gmass.co/verify?email=" + email + "&key=52D5D6DD-CD2B-4E5A-A76A-1667AEA3A6FC";
string response = await client.GetStringAsync(api);
var status = JsonConvert.DeserializeObject<dynamic>(response);
return status.Status;
}
}