简体   繁体   English

C# 在按钮上单击 API 值到标签

[英]C# on button click API value to Lable

I am trying to check if email is valid/invalid/unknown using GMASS API which sends "Status" as string value.我正在尝试使用发送“状态”作为字符串值的 GMASS API 检查 email 是否有效/无效/未知。

For example try these links:例如尝试这些链接:

Valid Email Response URL有效 Email 响应URL

Invalid Email Response URL无效 Email 响应URL

Retrieve "Status" value from JSON and set in Lable on Button click, How to achieve that?从 JSON 中检索“Status”值并在单击按钮时在 Lable 中设置,如何实现?

What I tried, Inside button this code:我试过什么,里面按钮这个代码:

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;
                }

What went wrong with my code?我的代码出了什么问题?

tested it in console app, was able to get status {invalid}在控制台应用程序中对其进行了测试,能够获得状态 {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;
    
                }

for the async task you can do it like this对于异步任务,你可以这样做
Note :It will take a little bit of time to get the result and update the label注意:获取结果和更新 label 需要一点时间

    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;

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM