Amazon.EC2.Util.EC2Metadata.GetItems C# (CSharp) Метод

GetItems() приватный статический Метод

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;
        }

Same methods

EC2Metadata::GetItems ( string path ) : IEnumerable
EC2Metadata::GetItems ( string path, int tries ) : IEnumerable

Usage Example

Пример #1
0
        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>());
            }
        }
All Usage Examples Of Amazon.EC2.Util.EC2Metadata::GetItems