Last active
October 20, 2017 18:36
-
-
Save jimmypc92/254d5315b861ab53a9ba12033169f9e5 to your computer and use it in GitHub Desktop.
HttpClient IIS Administration API demo
This file contains 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
namespace ConsoleAPIClient | |
{ | |
using System; | |
using System.Net; | |
using System.Net.Http; | |
using System.Security.Cryptography.X509Certificates; | |
class Program | |
{ | |
private const string SITES_ENDPOINT = "https://jimmyca-srv2.cloudapp.net:55539/api/webserver/websites"; | |
private const string CERT_THUMBPRINT = "2D1604329438A9D9E4B4FB461C2BD3A336CEB1D8"; | |
private const string TOKEN_HEADER_KEY = "Access-Token"; | |
private const string TOKEN_HEADER_VALUE = "Bearer OgMks6N7CtZTptX2DTnLe8JvkmATOuqw1ZJnZzK1RojeYs251Wlfvg"; | |
static void Main(string[] args) | |
{ | |
// Verify certificate being used for HTTPS | |
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { | |
X509Certificate2 apiCert = certificate as X509Certificate2; | |
return apiCert == null ? false : apiCert.Thumbprint == CERT_THUMBPRINT; | |
}; | |
using (var client = new HttpClient()) | |
{ | |
// Set access token for every request | |
client.DefaultRequestHeaders.Add(TOKEN_HEADER_KEY, TOKEN_HEADER_VALUE); | |
var res = client.GetAsync(SITES_ENDPOINT).Result; | |
if (res.StatusCode == HttpStatusCode.OK) | |
{ | |
Console.WriteLine(res.Content.ReadAsStringAsync().Result); | |
} | |
} | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment