Exemple #1
0
        Spell GetSpellData(string url)
        {
            if (string.IsNullOrWhiteSpace(url))
                throw new ArgumentNullException(nameof(url));

            Spell result = new Spell();

            try
            {
                var request = WebRequest.Create(url) as HttpWebRequest;
                using (var response = request.GetResponse() as HttpWebResponse)
                {
                    using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
                        var bytes = reader.ReadToEnd();
                        using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(bytes)))
                        {
                            var serializer = new DataContractJsonSerializer(typeof(Spell));
                            return serializer.ReadObject(stream) as Spell;
                        }
                    }
                }
            }
            catch (WebException wex)
                when (wex.Status == WebExceptionStatus.ProtocolError)
            {
                result.Id = spellId;
                result.Description = wex.Message;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return result;
        }
        public static Spell GetSpellData(uint spellId)
        {
            if (spellId == 0)
                throw new ArgumentNullException(nameof(spellId));

            var url = $"https://eu.api.battle.net/wow/spell/{spellId}?locale=ru_RU&apikey=ggj4gnyuywzcsdnehuznf6bjdvhfwfue";

            Spell result = new Spell();

            try
            {
                var request = WebRequest.Create(url) as HttpWebRequest;
                using (var response = request.GetResponse() as HttpWebResponse)
                {
                    using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
                        var bytes = reader.ReadToEnd();
                        using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(bytes)))
                        {
                            var serializer = new DataContractJsonSerializer(typeof(Spell));
                            return serializer.ReadObject(stream) as Spell;
                        }
                    }
                }
            }
            catch (WebException wex)
                when (wex.Status == WebExceptionStatus.ProtocolError)
            {
                result.Id = spellId;
                result.Description = wex.Message;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return result;
        }