Home > database >  C# on button click API value to Lable
C# on button click API value to Lable

Time:04-15

I am trying to check if email is valid/invalid/unknown using GMASS API which sends "Status" as string value.

For example try these links:

Valid Email Response URL

Invalid Email Response URL

Retrieve "Status" value from JSON and set in Lable on Button click, How to achieve that?

What I tried, Inside button this code:

textBox.Text = "[email protected]";
                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?

CodePudding user response:

tested it in console app, was able to get 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;
    
                }

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

    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;

        }
    }
  • Related