dir.by  
  Search  
Programming, development, testing
UWP (Universal Windows Platform) C# app for Windows
Write a UWP C# program that sends a Web API GET request to a local server
  Looked at 561 times    
 Write a UWP C# program that sends a Web API GET request to a local server 
last updated: 3 September 2025
Download an example:
AppUWP1_WebApi.zip ...
size: 22 kb
Step 1. Create a new C# UWP application
Step 2. In the MainPage.xaml.cs file, write a code to send a Web API request
To send a Web API request, I use the GetAsync method on the HttpClient class from the Microsoft standard library.
  C#  
using Windows.UI.Xaml.Controls;
using System.Threading.Tasks;
using System.Net.Http;
using System;


namespace AppUWP1
{
     public sealed partial class MainPage : Page
     {
          public MainPage()
          {
               this.InitializeComponent();

               Task.Run(async () =>
               {
                    await SendApiRequest();
               });


          }

          public async Task SendApiRequest()
          {
               try
               {
                    string url = "https://localhost:5001/my/books";

                    HttpClient api = new HttpClient();
                    var response = await api.GetAsync(url);
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                         var result = await response.Content.ReadAsStringAsync();
                    }
               }
               catch (Exception e)
               {
               }
          }

     }
}
Run the program. Error!
{"An error occurred while sending the request."}
{"The text associated with this error code could not be found.\r\n\r\nThe certificate authority is invalid or incorrect\r\n"}
Decision
Let's put the permission in the manifest:
• Internet (Client & Server)
• Internet (Client)
• Shared User Certificates
Run the program. Everything works
Response from the server Books: 'Walk in street', 'Sunny day'
Note!
I wrote a local server. This is a separate WEB Server C# application like this:
  C#  
[ApiController]
[Route("my")]
public class MyController : ControllerBase
{
     [HttpGet]
     [Route("books")]
     public IActionResult My1()
     {
          return Ok("Books: 'Walk in street', 'Sunny day1'");
     }
}
 
← Previous topic
Creating a new C# UWP app on a Windows computer
 
Next topic →
Download and install the library EPSON OPOS ADK (this library is needed to print on an Epson printer using C# code) | I have an EPSON TM-T20X receipt printer
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
Download and install Microsoft Visual Studio to write UWP applications on a computer with the Windows system
Open Visual Studio (to write the project UWP)
Creating a new C# UWP app on a Windows computer
Web API requests
Write a UWP C# program that sends a Web API GET request to a local server
Receipt printer (writing a C# program for printing)
Download and install the library EPSON OPOS ADK (this library is needed to print on an Epson printer using C# code) | I have an EPSON TM-T20X receipt printer
Adding a receipt EPSON printer to the EPSON OPOS ADK program
We write a program in UWP C# that prints text on a receipt EPSON printer (to find a receipt printer I use the C# code PointOfService.PosPrinter.GetDeviceSelector ...)
Write a program in UWP C# that prints text and images on a receipt EPSON printer
Write a program in UWP C# that prints an image of any size on a receipt EPSON POS printer (during execution, change the width of the picture = the width of the paper)
Write a program in UWP C# that prints a qr code on a receipt EPSON printer

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика