Rubix982 / Synet

A web proxy is written in ASP.NET with an admin console written in ReactJS. This was made as a repository for CN CS307's 1st assignment for web proxies @ FAST NUCES, Khi
https://synet-rubix982.vercel.app/app/dashboard
Apache License 2.0
0 stars 6 forks source link

Establishing HTTP connections #7

Closed Rubix982 closed 3 years ago

Rubix982 commented 3 years ago

Establish HTTP connections

Rubix982 commented 3 years ago

Some relevant links are,

  1. Call a Web API From a .NET Client (C#)
  2. Consume Web API in .NET using HttpClient
  3. Consume Web API Get method in ASP.NET MVC
Rubix982 commented 3 years ago

A great code example is here HttpClient Class, specifically, taking a look at the code example below,

// HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
static readonly HttpClient client = new HttpClient();

static async Task Main()
{
  // Call asynchronous network methods in a try/catch block to handle exceptions.
  try   
  {
     HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
     response.EnsureSuccessStatusCode();
     string responseBody = await response.Content.ReadAsStringAsync();
     // Above three lines can be replaced with new helper method below
     // string responseBody = await client.GetStringAsync(uri);

     Console.WriteLine(responseBody);
  }
  catch(HttpRequestException e)
  {
     Console.WriteLine("\nException Caught!");  
     Console.WriteLine("Message :{0} ",e.Message);
  }
}

It seems like this is written in some deprecated form - the current dotnet does not like static, readonly, and that async without a proper await.

Rubix982 commented 3 years ago

Done with commit# 92efaccdba29b982e44f6f2e85687d009c7e450b