Created
January 13, 2021 00:08
-
-
Save mpoerwito/7f04c1f1a3b5d4baf041b7ad90eeb88a to your computer and use it in GitHub Desktop.
HttpClient usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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