Integrating API in C #

0

I would like to pass the result of this API to a TextBox . I'm having trouble fetching information from this class to the graphics part.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Http;
namespace WindowsFormsApplication3
{
    static class Program
    {
        static async Task<string> LookupMac(string MacAddress)
        {
            var uri = new Uri("http://api.macvendors.com/" + WebUtility.UrlEncode(MacAddress));
            using (var wc = new HttpClient())
                return await wc.GetStringAsync(uri);
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {

            foreach (var mac in new string[] { "88:53:2E:67:07:BE", "FC:FB:FB:01:FA:21", "D4:F4:6F:C9:EF:8D" })
            Console.WriteLine(mac + "\t" + LookupMac(mac).Result);
            Console.ReadLine();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

        }
    }
}
    
asked by anonymous 22.04.2018 / 19:04

1 answer

0

You need to make your method public within the class Program ... but if it is not going to be accessed in all forms, I see no reason for it to be available here and not within the form's partial.

static class Program
{

    public static async Task<string> LookupMac(string MacAddress)
    {
        var uri = new Uri("http://api.macvendors.com/" + WebUtility.UrlEncode(MacAddress));
        using (var wc = new HttpClient())
            return await wc.GetStringAsync(uri);
    }

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

So you can run it from your form and build the content to display in textBox

public partial class Form1 : Form
 {

    public Form1()
    {
        StringBuilder sb = new StringBuilder();
        foreach (var mac in new string[] { "88:53:2E:67:07:BE", "FC:FB:FB:01:FA:21", "D4:F4:6F:C9:EF:8D" })
        {
            var result = Program.LookupMac(mac).Result;
            sb.AppendLine(string.Format("{0} \t {1}", mac, result));

        }

        textBox.Text = sb.ToString();
    }
}
    
13.11.2018 / 14:25