private void _BeginGetResponseCallback(IAsyncResult asynchronousResult) { RetrieveResult ret = null; MyRequest myRequestState = (MyRequest)asynchronousResult.AsyncState; try { // State of request is asynchronous. WebRequest myHttpWebRequest = myRequestState.Request; myRequestState.Response = (HttpWebResponse)myHttpWebRequest.EndGetResponse(asynchronousResult); // Read the response into a Stream object. Stream responseStream = myRequestState.Response.GetResponseStream(); myRequestState.Stream = responseStream; string metar = retr.DecodeWMOCode(responseStream); myRequestState.Stream.Close(); myRequestState.Response.Close(); ret = new RetrieveResult(metar); } catch (WebException ex) { ret = new RetrieveResult( new Exception("Failed to download data with metar.", ex)); } myRequestState.Finisher(ret); }
/// <summary> /// Used to download metar asynchronously. /// </summary> private void DownloadAsynchronously() { string icao = aIcao; RetrieveResult ret = null; WebRequest req = HttpWebRequest.Create( retr.GetUrlForICAO(icao)); MyRequest mr = new MyRequest() { Request = req, Finisher = aDel }; try { req.BeginGetResponse(new AsyncCallback(_BeginGetResponseCallback), mr); } catch (Exception ex) { ret = new RetrieveResult( new DownloadException("Failed to download metar from web.", ex)); } if (ret != null) { mr.Finisher(ret); } }