/// <summary> /// Asynchronously retrieve the response from the requested url to the specified response stream. /// </summary> public void GetResponseAsync(string url, Stream responseStream, string accept, HttpPostData postData, WebRequestAsyncState state) { m_asyncState = state; m_asyncState.Request = this; if (Dispatcher.IsMultiThreaded) { GetResponseDelegate caller = GetResponse; caller.BeginInvoke(url, responseStream, accept, postData, GetResponseAsyncCompleted, caller); } else { GetResponseAsyncCompletedCore(() => GetResponse(url, responseStream, accept, postData)); } }
/// <summary> /// Callback method for asynchronous requests /// </summary> private void DownloadStringAsyncCompleted(WebRequestAsyncState state) { StringRequestAsyncState requestState = (StringRequestAsyncState)state; string result = String.Empty; if (!requestState.Request.Cancelled && requestState.Error == null && requestState.Request.ResponseStream != null) { requestState.Request.ResponseStream.Seek(0, SeekOrigin.Begin); using (StreamReader reader = new StreamReader(requestState.Request.ResponseStream)) { result = reader.ReadToEnd(); } } if (requestState.Request.ResponseStream != null) { requestState.Request.ResponseStream.Close(); } requestState.DownloadStringCompleted(new DownloadStringAsyncResult(result, requestState.Error), requestState.UserState); }
/// <summary> /// Callback method for asynchronous requests /// </summary> private void DownloadStringAsyncCompleted(WebRequestAsyncState state) { StringRequestAsyncState requestState = (StringRequestAsyncState)state; string result = string.Empty; if (!requestState.Request.Cancelled && requestState.Error == null && requestState.Request.ResponseStream != null) { requestState.Request.ResponseStream.Seek(0, SeekOrigin.Begin); using (StreamReader reader = new StreamReader(requestState.Request.ResponseStream)) { result = reader.ReadToEnd(); } } if (requestState.Request.ResponseStream != null) { requestState.Request.ResponseStream.Close(); } requestState.DownloadStringCompleted(new DownloadStringAsyncResult(result, requestState.Error), requestState.UserState); }
/// <summary> /// Callback method for asynchronous requests /// </summary> private void DownloadFileAsyncCompleted(WebRequestAsyncState state) { FileRequestAsyncState requestState = (FileRequestAsyncState)state; FileInfo fileResult = null; if (!requestState.Request.Cancelled && requestState.Error == null) { try { fileResult = new FileInfo(requestState.FilePath); } catch (Exception ex) { requestState.Error = HttpWebServiceException.FileError(requestState.Request.BaseUrl, ex); } } if (requestState.Request.ResponseStream != null) { requestState.Request.ResponseStream.Close(); } requestState.DownloadFileCompleted(new DownloadFileAsyncResult(fileResult, requestState.Error, requestState.Request.Cancelled)); }
/// <summary> /// Callback method for asynchronous requests /// </summary> private void DownloadXmlAsyncCompleted(WebRequestAsyncState state) { XmlRequestAsyncState requestState = (XmlRequestAsyncState)state; XmlDocument xdocResult = new XmlDocument(); if (!requestState.Request.Cancelled && requestState.Error == null && requestState.Request.ResponseStream != null) { try { requestState.Request.ResponseStream.Seek(0, SeekOrigin.Begin); xdocResult.Load(requestState.Request.ResponseStream); } catch (XmlException ex) { requestState.Error = HttpWebServiceException.XmlException(requestState.Request.BaseUrl, ex); } } if (requestState.Request.ResponseStream != null) { requestState.Request.ResponseStream.Close(); } requestState.DownloadXmlCompleted(new DownloadXmlAsyncResult(xdocResult, requestState.Error), requestState.UserState); }
/// <summary> /// Callback method for asynchronous requests /// </summary> private void DownloadImageAsyncCompleted(WebRequestAsyncState state) { ImageRequestAsyncState requestState = (ImageRequestAsyncState)state; Image imageResult = null; try { if (!requestState.Request.Cancelled && requestState.Error == null) { imageResult = GetImage(requestState.Request); } } catch (HttpWebServiceException ex) { requestState.Error = ex; if (requestState.Request.ResponseStream != null) { requestState.Request.ResponseStream.Close(); } } requestState.DownloadImageCompleted(new DownloadImageAsyncResult(imageResult, requestState.Error), requestState.UserState); }