How do I call a minimal API on a remote machine from a winform c# program?
Loading
How do I call a minimal API on a remote machine from a winform c# program?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tahir AnsariRank #366 · 6.8K reputationPosted Aug 2, 2023, 11:33 PM EDT
Try below example
1) Add HttpClient to your WinForms Project: In your WinForms C# project, add a reference to
System.Net.Httpby right-clicking on the project, selecting "Manage NuGet Packages,"2) Write Code to Call the Remote API: In your WinForms code, you can use the
HttpClientclass to call the remote API.using System; using System.Net.Http; using System.Threading.Tasks; namespace WinFormsApp { public partial class Form1 : Form { private readonly HttpClient httpClient; public Form1() { InitializeComponent(); httpClient = new HttpClient(); } private async void btnCallApi_Click(object sender, EventArgs e) { try { string apiUrl = "http://REMOTE_MACHINE_IP/api/endpoint"; // Replace with the actual API URL HttpResponseMessage response = await httpClient.GetAsync(apiUrl); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); // Process the response data here, display it on the WinForms UI, etc. textBoxResponse.Text = responseBody; } catch (HttpRequestException ex) { // Handle exceptions, e.g., API not accessible, server error, etc. MessageBox.Show($"Error: {ex.Message}", "API Call Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }Cr BhargaviRank #420 · 5.9K reputationPosted Aug 2, 2023, 9:08 PM EDT
To call a minimal API on a remote machine from a WinForms C# program, you can use the
HttpClientclass to make HTTP requests to the API endpoint