/// <summary>
        /// Synchronously downloads a string from the specified url
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public String DownloadString(string url)
        {
            string urlValidationError;

            if (!IsValidURL(url, out urlValidationError))
            {
                throw new ArgumentException(urlValidationError);
            }
            HttpWebServiceRequest request = GetRequest();

            try
            {
                request.GetResponse(url, new MemoryStream(), STRING_ACCEPT);
                string result = String.Empty;
                if (request.ResponseStream != null)
                {
                    request.ResponseStream.Seek(0, SeekOrigin.Begin);
                    using (StreamReader reader = new StreamReader(request.ResponseStream))
                    {
                        result = reader.ReadToEnd();
                    }
                }
                return(result);
            }
            finally
            {
                if (request.ResponseStream != null)
                {
                    request.ResponseStream.Close();
                }
            }
        }
        /// <summary>
        /// Downloads an Xml document from the specified url using the specified POST data
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <returns></returns>
        public XmlDocument DownloadXml(string url, HttpPostData postData)
        {
            string urlValidationError;

            if (!IsValidURL(url, out urlValidationError))
            {
                throw new ArgumentException(urlValidationError);
            }
            HttpWebServiceRequest request = GetRequest();

            try
            {
                request.GetResponse(url, new MemoryStream(), XML_ACCEPT, postData);
                XmlDocument result = new XmlDocument();
                if (request.ResponseStream != null)
                {
                    request.ResponseStream.Seek(0, SeekOrigin.Begin);
                    result.Load(request.ResponseStream);
                }
                return(result);
            }
            finally
            {
                if (request.ResponseStream != null)
                {
                    request.ResponseStream.Close();
                }
            }
        }
        /// <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>
        /// Asynchronously downloads a string from the specified url
        /// </summary>
        /// <param name="url"></param>
        /// <param name="callback">A <see cref="DownloadXmlCompletedCallback"/> to be invoked when the request is completed</param>
        /// <param name="userState">A state object to be returned to the callback</param>
        /// <returns></returns>
        public object DownloadStringAsync(string url, DownloadStringCompletedCallback callback, object userState)
        {
            string urlValidationError;

            if (!IsValidURL(url, out urlValidationError))
            {
                throw new ArgumentException(urlValidationError);
            }
            StringRequestAsyncState state   = new StringRequestAsyncState(callback, DownloadStringAsyncCompleted, userState);
            HttpWebServiceRequest   request = GetRequest();

            request.GetResponseAsync(url, new MemoryStream(), STRING_ACCEPT, null, state);
            return(request);
        }
        /// <summary>
        /// Asynchronously downloads file from the specified url to the specified path
        /// </summary>
        /// <param name="url"></param>
        /// <param name="filePath"></param>
        /// <param name="callback">A <see cref="DownloadImageCompletedCallback"/> to be invoked when the request is completed</param>
        /// <param name="progressCallback"></param>
        /// <returns></returns>
        public object DownloadFileAsync(string url, string filePath, DownloadFileCompletedCallback callback, DownloadProgressChangedCallback progressCallback)
        {
            string urlValidationError;

            if (!IsValidURL(url, out urlValidationError))
            {
                throw new ArgumentException(urlValidationError);
            }
            FileRequestAsyncState state   = new FileRequestAsyncState(filePath, callback, progressCallback, DownloadFileAsyncCompleted);
            HttpWebServiceRequest request = GetRequest();

            request.GetResponseAsync(url, new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None), IMAGE_ACCEPT, null, state);
            return(request);
        }
        /// <summary>
        /// Asynchronously downloads an xml file from the specified url
        /// </summary>
        /// <param name="url"></param>
        /// <param name="callback">A <see cref="DownloadXmlCompletedCallback"/> to be invoked when the request is completed</param>
        /// <param name="userState">A state object to be returned to the callback</param>
        /// <returns></returns>
        public object DownloadXmlAsync(string url, HttpPostData postData, DownloadXmlCompletedCallback callback, object userState)
        {
            string urlValidationError;

            if (!IsValidURL(url, out urlValidationError))
            {
                throw new ArgumentException(urlValidationError);
            }

            XmlRequestAsyncState  state   = new XmlRequestAsyncState(callback, DownloadXmlAsyncCompleted, userState);
            HttpWebServiceRequest request = GetRequest();

            request.GetResponseAsync(url, new MemoryStream(), XML_ACCEPT, postData, state);
            return(request);
        }
Beispiel #7
0
        /// <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);
        }
Beispiel #8
0
        /// <summary>
        /// Downloads an image from the specified url
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public Image DownloadImage(string url)
        {
            string urlValidationError;

            if (!IsValidURL(url, out urlValidationError))
            {
                throw new ArgumentException(urlValidationError);
            }
            HttpWebServiceRequest request = GetRequest();

            try
            {
                request.GetResponse(url, new MemoryStream(), IMAGE_ACCEPT);
                return(GetImage(request));
            }
            catch (Exception)
            {
                if (request.ResponseStream != null)
                {
                    request.ResponseStream.Close();
                }
                throw;
            }
        }