Skip to content

Instantly share code, notes, and snippets.

@mpoerwito
Created January 13, 2021 00:08
Show Gist options
  • Save mpoerwito/7f04c1f1a3b5d4baf041b7ad90eeb88a to your computer and use it in GitHub Desktop.
Save mpoerwito/7f04c1f1a3b5d4baf041b7ad90eeb88a to your computer and use it in GitHub Desktop.
HttpClient usage
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Text.Json;
namespace runthis
{
class Program
{
static void Main(string[] args)
{
Task t = new Task(GetRequest);
t.Start();
Console.WriteLine("Start GetRequest...");
Console.ReadLine();
}
static async void PostRequest()
{
}
static async void GetRequest()
{
UriBuilder ub = new UriBuilder {
Host = "mediiii.xyz",
Port = -1,
Path = "/api/getquotebytag/time"
};
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(ub.ToString()))
if (response.IsSuccessStatusCode)
{
using (HttpContent content = response.Content)
{
string result = await content.ReadAsStringAsync();
if (result != null )
{
Console.WriteLine($"URI: {ub.ToString()}");
List<Quote> qs = JsonSerializer.Deserialize<List<Quote>>(result);
foreach(Quote q in qs)
{
Console.WriteLine($"{q.words} ~{q.author}");
}
}
}
}
else
{
Console.WriteLine(response.StatusCode);
}
}
}
class Quote
{
public int id { get; set; }
public string words { get; set; }
public string author { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment