コード例 #1
0
 public static void QueryCurrentWeather(int zipCode)
 {
     if (!IsActive)
     {
         ZipCode zipCodeDb = ZipCode.Get(zipCode);
         if (zipCodeDb != null)
         {
             ZipCodeCity zipCodeCity = ZipCodeCity.Get(zipCodeDb.CityId);
             ServerUri = "http://forecast.weather.gov/MapClick.php?lat=" +
                         zipCodeCity.Latitude + "&lon=" +
                         zipCodeCity.Longitude + "&site=all&smap=1";
             CurrentZipCode     = zipCode;
             IsActive           = true;
             CurrentWeather     = "";
             CurrentTemperature = -1000;
             BytesReceived      = 0;
             DownloadThread     = new Thread(DownloadWebFileStart);
             DownloadThread.Start();
         }
         else
         {
             if (QueryFailed != null)
             {
                 QueryFailed.Invoke(typeof(WeatherHelper), new EventArgs());
             }
         }
     }
 }
コード例 #2
0
        private static void DownloadWebFileStart()
        {
            string         pageText = "";
            HttpWebRequest request  = WebRequest.Create(ServerUri) as HttpWebRequest;

            if (request == null)
            {
                return;
            }
            try
            {
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                if (response != null)
                {
                    using (Stream input = response.GetResponseStream())
                    {
                        int    bytesRead;
                        byte[] buffer = new byte[BufferSize];
                        while (input != null && (bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            BytesReceived += bytesRead;
                            pageText      += Encoding.ASCII.GetString(buffer, 0, bytesRead);
                        }
                    }
                }
                request.Abort();
                DispatchingWindow.Dispatcher.Invoke((Action)(() =>
                {
                    try
                    {
                        ParsePageText(pageText);
                        if (QueryCompleted != null)
                        {
                            QueryCompleted.Invoke(typeof(WeatherHelper), new EventArgs());
                        }
                    }
                    catch
                    {
                        if (QueryFailed != null)
                        {
                            QueryFailed.Invoke(typeof(WeatherHelper), new EventArgs());
                        }
                    }
                }));
                IsActive = false;
            }
            catch (WebException)
            {
                request.Abort();
                IsActive = false;
            }
        }