/// <summary> /// Downloads an file from the specified url to the specified path /// </summary> /// <param name="url"></param> /// <param name="filePath"></param> /// <returns></returns> public FileInfo DownloadFile(string url, string filePath) { string urlValidationError; if (!IsValidURL(url, out urlValidationError)) { throw new ArgumentException(urlValidationError); } HttpWebServiceRequest request = GetRequest(); try { FileStream responseStream; try { responseStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None); } catch (Exception ex) { throw HttpWebServiceException.FileError(url, ex); } request.GetResponse(url, responseStream, FILE_ACCEPT); return(new FileInfo(filePath)); } finally { if (request.ResponseStream != null) { request.ResponseStream.Close(); } } }
/// <summary> /// Gets a redirected HttpWebResponse. /// </summary> private HttpWebResponse GetRedirectedHttpResponse(string target) { if (m_redirectsRemaining-- > 0) { Uri referer = new Uri(m_url); m_referer = referer.ToString(); m_url = new Uri(referer, target).ToString(); return(GetHttpResponse()); } else { throw HttpWebServiceException.RedirectsExceededException(BaseUrl); } }
/// <summary> /// Helper method to return an Image from the completed request /// </summary> private Image GetImage(HttpWebServiceRequest request) { Image result = null; if (request.ResponseStream != null) { request.ResponseStream.Seek(0, SeekOrigin.Begin); try { result = Image.FromStream(request.ResponseStream, true); } catch (ArgumentException ex) { throw HttpWebServiceException.ImageException(request.BaseUrl, ex); } } return(result); }
/// <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); }
public DownloadImageAsyncResult(Image image, HttpWebServiceException error) { _error = error; _result = image; }
public DownloadFileAsyncResult(FileInfo file, HttpWebServiceException error, bool cancelled) { _error = error; _cancelled = cancelled; _result = file; }
/// <summary> /// Retrieve the response from the reguested URL to the specified response stream /// If postData is supplied, the request is submitted as a POST request, otherwise it is submitted as a GET request /// The download process is broken into chunks for future implementation of asynchronous requests /// </summary> internal void GetResponse(string url, Stream responseStream, string accept, HttpPostData postData) { // Store params m_url = url; m_baseUrl = url; m_accept = accept; m_postData = postData; m_responseStream = responseStream; Stream webResponseStream = null; HttpWebResponse webResponse = null; try { webResponse = GetHttpResponse(); webResponseStream = webResponse.GetResponseStream(); int bytesRead; long totalBytesRead = 0; long rawBufferSize = webResponse.ContentLength / 100; int bufferSize = (int)(rawBufferSize > m_webServiceState.MaxBufferSize ? m_webServiceState.MaxBufferSize : (rawBufferSize < m_webServiceState.MinBufferSize ? m_webServiceState.MinBufferSize : rawBufferSize)); do { byte[] buffer = new byte[bufferSize]; bytesRead = webResponseStream.Read(buffer, 0, bufferSize); if (bytesRead > 0) { m_responseStream.Write(buffer, 0, bytesRead); if (m_asyncState != null && m_asyncState.ProgressCallback != null) { totalBytesRead += bytesRead; int progressPercentage = webResponse.ContentLength == 0 ? 0 : (int)((totalBytesRead * 100) / webResponse.ContentLength); m_asyncState.ProgressCallback(new DownloadProgressChangedArgs(webResponse.ContentLength, totalBytesRead, progressPercentage)); } } } while (bytesRead > 0 && !Cancelled); } catch (HttpWebServiceException) { throw; } catch (WebException ex) { // Aborted, time out or error while processing the request throw HttpWebServiceException.WebException(BaseUrl, m_webServiceState, ex); } catch (Exception ex) { throw HttpWebServiceException.Exception(url, ex); } finally { if (webResponseStream != null) { webResponseStream.Close(); } if (webResponse != null) { webResponse.Close(); } } }
public DownloadXmlAsyncResult(XmlDocument xdoc, HttpWebServiceException error) { _error = error; _result = xdoc; }