private Dictionary <string, string> FindCity(string city)
        {
            Dictionary <string, string> outputDict = new Dictionary <string, string>();
            string URL  = @"http://api.openweathermap.org/data/2.5/weather?q=" + city.Trim() + @"&APPID=" + Setting.APPID + "&units=metric";
            string json = String.Empty;

            //HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
            HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;

            request.UserAgent = "Googlebot/1.0 ([email protected] http://googlebot.com/)";

            if (!Setting.UseDefaultProxy)
            {
                WebProxy proxy = new WebProxy(Setting.ProxyURL, Setting.ProxyPort);
                request.Proxy = proxy;
            }
            else
            {
                IWebProxy proxy = WebRequest.GetSystemWebProxy();
                request.Proxy = proxy;
            }

            if (request.Proxy != null)
            {
                request.Proxy.Credentials = CredentialCache.DefaultCredentials;
            }

            try
            {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        var encoding = ASCIIEncoding.ASCII;
                        using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
                        {
                            string responseText = reader.ReadToEnd();
                            WeatherApp.JSON_Classes.RootObject rootObject = JsonConvert.DeserializeObject <WeatherApp.JSON_Classes.RootObject>(responseText);
                            outputDict.Add("found", "true");
                            outputDict.Add("name", rootObject.name.ToString());
                            outputDict.Add("id", rootObject.id.ToString());
                            outputDict.Add("lat", rootObject.coord.lat.ToString());
                            outputDict.Add("lon", rootObject.coord.lon.ToString());
                            return(outputDict);
                        }
                    }
                }
            }
            catch (System.Net.WebException ex)
            {
                outputDict.Add("found", "false");
                outputDict.Add("error description", ex.Message);
                return(outputDict);
            }
        }
Beispiel #2
0
        private Task <Dictionary <string, string> > GetCurrentWeatherData(string ID, string apikey)
        {
            var outputDict = new Dictionary <string, string>();
            //Dictionary<string, string> outputDict = new Dictionary<string, string>();
            //string URL = @"http://api.openweathermap.org/data/2.5/weather?id=" + ID + @"&APPID=" + Setting.APPID + "&units=metric";
            string URL  = $"http://api.openweathermap.org/data/2.5/weather?id={ID}&APPID={apikey}&units=metric";
            string json = String.Empty;

            HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;

            request.UserAgent = "Googlebot/1.0 ([email protected] http://googlebot.com/)";

            if (!Setting.UseDefaultProxy)
            {
                WebProxy proxy = new WebProxy(Setting.ProxyURL, Setting.ProxyPort);
                request.Proxy = proxy;
            }
            else
            {
                IWebProxy proxy = WebRequest.GetSystemWebProxy();
                request.Proxy = proxy;
            }

            if (request.Proxy != null)
            {
                request.Proxy.Credentials = CredentialCache.DefaultCredentials;
            }

            try
            {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        var encoding = ASCIIEncoding.ASCII;
                        using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
                        {
                            string responseText = reader.ReadToEnd();
                            WeatherApp.JSON_Classes.RootObject rootObject = JsonConvert.DeserializeObject <WeatherApp.JSON_Classes.RootObject>(responseText);

                            string temp        = String.Empty;
                            string temp_min    = String.Empty;
                            string temp_max    = String.Empty;
                            string pressure    = String.Empty;
                            string humidity    = String.Empty;
                            string main        = String.Empty;
                            string description = String.Empty;
                            string speed       = String.Empty;
                            string deg         = String.Empty;

                            outputDict.Add("found", "true");

                            #region Main
                            try { temp = rootObject.main.temp.ToString(); } catch (NullReferenceException) { }
                            outputDict.Add("temp", temp);
                            try { pressure = rootObject.main.pressure.ToString(); } catch (NullReferenceException) { }
                            outputDict.Add("pressure", pressure);
                            try { humidity = rootObject.main.humidity.ToString(); } catch (NullReferenceException) { }
                            outputDict.Add("humidity", humidity);
                            try { temp_min = rootObject.main.temp_min.ToString(); } catch (NullReferenceException) { }
                            outputDict.Add("temp_min", temp_min);
                            try { temp_max = rootObject.main.temp_max.ToString(); } catch (NullReferenceException) { }
                            outputDict.Add("temp_max", temp_max);
                            #endregion

                            #region Wind
                            try { speed = rootObject.wind.speed.ToString(); } catch (NullReferenceException) { }
                            outputDict.Add("speed", speed);
                            try { deg = rootObject.wind.deg.ToString(); } catch (NullReferenceException) { }
                            outputDict.Add("deg", deg);
                            #endregion

                            #region Weather
                            try { main = rootObject.weather[0].main.ToString(); } catch (NullReferenceException) { }
                            outputDict.Add("main", main);
                            try { description = rootObject.weather[0].description.ToString(); } catch (NullReferenceException) { }
                            outputDict.Add("description", description);
                            #endregion

                            return(Task.FromResult <Dictionary <string, string> >(outputDict));
                        }
                    }
                }
            }
            catch (System.Net.WebException ex)
            {
                return(null);
            }
        }