Index.razor
@page "/"
<PageTitle>Index</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<button @onclick="@SendData">Send</button>
@code {
public async Task SendData()
{
Console.WriteLine(await API.Send());
}
}
API.cs
using Microsoft.AspNetCore.Components.WebAssembly.Http;
using System.Net.Sockets;
namespace TestBlazorWebApp
{
public static class API
{
public static async Task<string> Send()
{
string json = "data";
var request = new HttpRequestMessage(HttpMethod.Post, "https://...");
request.Content = new StringContent(json);
request.SetBrowserRequestMode(BrowserRequestMode.NoCors);
using (var client = new HttpClient())
{
var response = await client.SendAsync(request).ConfigureAwait(true);
bool isSuccessCode = response.IsSuccessStatusCode;
string responseStr = await response.Content.ReadAsStringAsync().ConfigureAwait(true);
return responseStr;
}
}
}
}