private static GetItems ( string path, int tries, bool slurp ) : List |
||
path | string | |
tries | int | |
slurp | bool | |
리턴 | List |
private static List<string> GetItems(string path, int tries, bool slurp)
{
var items = new List<string>();
try
{
HttpWebRequest request;
if (path.StartsWith("http", StringComparison.Ordinal))
request = WebRequest.Create(path) as HttpWebRequest;
else
request = WebRequest.Create(EC2_METADATA_ROOT + path) as HttpWebRequest;
request.Timeout = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
using (var response = request.GetResponse())
{
using (var stream = new StreamReader(response.GetResponseStream()))
{
if (slurp)
items.Add(stream.ReadToEnd());
else
{
string line;
do
{
line = stream.ReadLine();
if (line != null)
items.Add(line.Trim());
}
while (line != null);
}
}
}
}
catch (WebException wex)
{
var response = wex.Response as HttpWebResponse;
if (response != null && response.StatusCode == HttpStatusCode.NotFound)
return null;
if (tries <= 1)
{
Logger.GetLogger(typeof(Amazon.EC2.Util.EC2Metadata)).Error(wex, "Unable to contact EC2 Metadata service.");
return null;
}
PauseExponentially(tries);
return GetItems(path, tries - 1, slurp);
}
return items;
}
private IEnumerable <string> GetItems(string key) { if (null == _availableKeys) { _availableKeys = EC2Metadata.GetItems(_path); } if (_availableKeys.Contains(key)) { return(EC2Metadata.GetItems(_path + key)); } else { return(new List <string>()); } }