示例#1
0
        /// <summary>
        /// Download metar synchronously.
        /// </summary>
        /// <param name="ICAO">Icao code of airport/station.</param>
        /// <returns>Metar as string.</returns>
        /// <exception cref="DownloadException">
        /// Raised when any error occurs.
        /// </exception>
        public string Download(string ICAO)
        {
            string ret = "";

            WebRequest req = HttpWebRequest.Create(
                retr.GetUrlForICAO(ICAO));

            try
            {
                WebResponse resp = req.GetResponse();

                System.IO.Stream respStream = resp.GetResponseStream();

                ret = retr.DecodeWMOCode(respStream);

                respStream.Close();
                resp.Close();
            }
            catch (Exception ex)
            {
                throw new DownloadException("Failed to download metar from web.", ex);
            }

            return(ret);
        }
示例#2
0
        /// <summary>
        /// Download metar asynchronously.
        /// </summary>
        /// <param name="icao">Icao code of airport/station.</param>
        /// <param name="handler">The HTTP handler stack to use for sending requests.</param>
        /// <param name="disposeHandler">The value indicating whether the <paramref name="handler"/> must be disposed after the operation completes.</param>
        /// <exception cref="ArgumentNullException"><paramref name="icao"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="handler"/> is <see langword="null"/>.</exception>
        public async Task <string> DownloadAsync(
            string icao,
            HttpMessageHandler handler,
            bool disposeHandler)
        {
            if (icao == null)
            {
                throw new ArgumentNullException(nameof(icao));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            var url        = _retriever.GetUrlForICAO(icao);
            var httpClient = new HttpClient(handler, disposeHandler);

            try
            {
                using (var response = await httpClient.GetStreamAsync(url).ConfigureAwait(false))
                {
                    return(await _retriever.DecodeWMOCodeAsync(response).ConfigureAwait(false));
                }
            }
            catch (Exception ex)
            {
                throw new DownloadException("Failed to download metar from web.", ex);
            }
            finally
            {
                httpClient.Dispose();
            }
        }