call-simple-rest

CSharp · Console

/Program.cs

using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
					
public class Program
{
	// Always use HttpClient as a singleton object
	public static HttpClient _httpClient = new HttpClient() { BaseAddress = new Uri("http://al1b.pythonanywhere.com") } ;
	public async static Task Main()
	{
		
		var num1 = 1;
		var num2 = 4;
		
		Console.WriteLine("Making http-request wait ...\r\n");		
	
		var mathAddResult = await _httpClient.GetAsync($"/math/add/{num1}/{num2}");

		
		// 200 = OK
		if(mathAddResult.StatusCode == HttpStatusCode.OK)
		{
			// to understand http-status codes take a look on this website: https://http.cat/			
			Console.WriteLine(await mathAddResult.Content.ReadAsStringAsync());
		}
	}
}
;