public class WebConnect
{
// Console: Install-Package Microsoft.AspNet.WebApi.Client
// Create and initialize HttpClient, using System.Net.Http;
public static System.Net.Http.HttpClient webclient = new System.Net.Http.HttpClient();
/* HttpClient is intended to be instantiated once and re-used throughout the life of an application.
* Especially in server applications, creating a new HttpClient instance for every request will
* exhaust the number of sockets available under heavy loads. This will result in SocketException errors. */
// Method Initialize the HttpClient
public static void InitHttpClient(string url)
{
try
{
/* Connect and set accept header to "application/json" which tells the server to
* send data in JSON format */
webclient.BaseAddress = new Uri(url);
webclient.DefaultRequestHeaders.Accept.Clear();
webclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
webclient.GetAsync(url);
}
catch(Exception e)
{
throw new ArgumentException(e.Message);
}
}
}