Skip to content

Instantly share code, notes, and snippets.

@nordineb
Last active October 20, 2017 13:18
Show Gist options
  • Save nordineb/7340c7e11d3eef6abbbb9f68c5fabc45 to your computer and use it in GitHub Desktop.
Save nordineb/7340c7e11d3eef6abbbb9f68c5fabc45 to your computer and use it in GitHub Desktop.
cloud-powershell
namespace Samples
{
using System;
using System.Net.Http;
class IMDSSample
{
// Query IMDS server and retrieve JSON result
private static string JsonQueryIMDS(string path)
{
const string api_version = "2017-04-02";
const string imds_server = "169.254.169.254";
string imdsUri = "http://" + imds_server + "/metadata" + path + "?api-version=" + api_version;
string jsonResult = string.Empty;
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Metadata", "True");
try
{
jsonResult = httpClient.GetStringAsync(imdsUri).Result;
}
catch (AggregateException ex)
{
// handle response failures
Console.WriteLine("Request failed: " + ex.InnerException.Message);
}
}
return jsonResult;
}
static void Main(string[] args)
{
// query /instance metadata
var result = JsonQueryIMDS("/instance");
// display raw json
Console.WriteLine(result);
// parse json result
// ...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment