-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathProgram.cs
52 lines (46 loc) · 1.38 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using Wasi.Http;
using System.Net.Http;
using System.Text;
using System.Text.Json;
class HttpWasm
{
static void printResponse(HttpResponseMessage response)
{
Console.WriteLine(response);
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
}
static async Task testPost(HttpClient client)
{
// taken from https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient
using StringContent jsonContent = new(
JsonSerializer.Serialize(new
{
userId = 77,
id = 1,
title = "write code sample",
completed = false
}),
Encoding.UTF8,
"application/json");
using var response = await client.PostAsync("https://postman-echo.com/post", jsonContent);
printResponse(response);
}
static async Task testGet(HttpClient client)
{
using var result = await client.GetAsync("https://postman-echo.com/get", System.Threading.CancellationToken.None);
printResponse(result);
}
static async Task Main(string[] args)
{
var client = new HttpClient(new WasiHttpHandler());
try
{
await testGet(client);
await testPost(client);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}