/// <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>
        /// Asynchronously downloads an xml file from the specified url.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="method">The method.</param>
        /// <param name="acceptEncoded">if set to <c>true</c> accept encoded response.</param>
        /// <param name="postdata">The post data.</param>
        /// <param name="dataCompression">The post data compression method.</param>
        public static async Task <DownloadResult <IXPathNavigable> > DownloadXmlAsync(Uri url, HttpMethod method = null,
                                                                                      bool acceptEncoded         = false, string postdata = null, DataCompression dataCompression = DataCompression.None)
        {
            string urlValidationError;

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

            HttpPostData             postData = string.IsNullOrWhiteSpace(postdata) ? null : new HttpPostData(postdata, dataCompression);
            HttpClientServiceRequest request  = new HttpClientServiceRequest();

            request.AuthToken       = null;
            request.AcceptEncoded   = acceptEncoded;
            request.DataCompression = dataCompression;
            try
            {
                HttpResponseMessage response = await request.SendAsync(url, method, postData,
                                                                       XmlAccept).ConfigureAwait(false);

                using (response)
                {
                    Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                    return(GetXmlDocument(request.BaseUrl, stream, response));
                }
            }
            catch (HttpWebClientServiceException ex)
            {
                return(new DownloadResult <IXPathNavigable>(new XmlDocument(), ex));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Asynchronously downloads an object (streaming) from the specified url.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="acceptEncoded">if set to <c>true</c> accept encoded response.</param>
        /// <param name="postData">The post data. If null, GET will be used.</param>
        /// <param name="parser">The function which will parse the stream.</param>
        /// <param name="token">The ESI token, or null if none is used.</param>
        public static async Task <DownloadResult <T> > DownloadStreamAsync <T>(Uri url,
                                                                               ParseDataDelegate <T> parser, bool acceptEncoded = false,
                                                                               HttpPostData postData = null, string token = null)
        {
            string urlValidationError;

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

            HttpClientServiceRequest request = new HttpClientServiceRequest();

            request.AuthToken       = token;
            request.AcceptEncoded   = acceptEncoded;
            request.DataCompression = postData?.Compression ?? DataCompression.None;
            try
            {
                var response = await request.SendAsync(url, (postData == null)?
                                                       HttpMethod.Get : HttpMethod.Post, postData, StreamAccept).
                               ConfigureAwait(false);

                using (response)
                {
                    var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                    return(GetResult(url, stream, parser, response));
                }
            }
            catch (HttpWebClientServiceException ex)
            {
                return(new DownloadResult <T>(default(T), ex));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Asynchronously downloads a string from the specified url.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="method">The method.</param>
        /// <param name="acceptEncoded">if set to <c>true</c> accept encoded response.</param>
        /// <param name="postdata">The post data.</param>
        /// <param name="dataCompression">The post data compression method.</param>
        public static async Task <DownloadResult <String> > DownloadStringAsync(Uri url, HttpMethod method = null, bool acceptEncoded              = false,
                                                                                string postdata            = null, DataCompression dataCompression = DataCompression.None)
        {
            string urlValidationError;

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

            HttpPostData             postData = String.IsNullOrWhiteSpace(postdata) ? null : new HttpPostData(postdata, dataCompression);
            HttpClientServiceRequest request  = new HttpClientServiceRequest();

            try
            {
                HttpResponseMessage response =
                    await request.SendAsync(url, method, postData, dataCompression, acceptEncoded, StringAccept).ConfigureAwait(false);

                using (response)
                {
                    Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                    return(GetString(request.BaseUrl, stream));
                }
            }
            catch (HttpWebClientServiceException ex)
            {
                return(new DownloadResult <String>(String.Empty, ex));
            }
        }
        /// <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;
        }
        /// <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);
        }
 /// <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();
     }
 }
Exemplo n.º 8
0
 /// <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));
     }
 }
Exemplo n.º 9
0
        /// <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();
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Asynchronously sends a request to the specified url.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="method">The method.</param>
        /// <param name="postData">The post data.</param>
        /// <param name="dataCompression">The data compression.</param>
        /// <param name="acceptEncoded">if set to <c>true</c> accept encoded response.</param>
        /// <param name="accept">The accept.</param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> SendAsync(Uri url, HttpMethod method, HttpPostData postData,
            DataCompression dataCompression,
            bool acceptEncoded, string accept)
        {
            while (true)
            {
                // Store params
                m_url = url;
                m_accept = accept;
                m_postData = postData;
                m_method = postData == null || method == null ? HttpMethod.Get : method;
                m_dataCompression = postData == null ? DataCompression.None : dataCompression;
                m_acceptEncoded = acceptEncoded;

                HttpResponseMessage response = null;
                try
                {
                    HttpClientHandler httpClientHandler = GetHttpClientHandler();
                    HttpRequestMessage request = GetHttpRequest();
                    response = await GetHttpResponseAsync(httpClientHandler, request).ConfigureAwait(false);

                    EnsureSuccessStatusCode(response);
                }
                catch (HttpWebClientServiceException)
                {
                    throw;
                }
                catch (HttpRequestException ex)
                {
                    if (ex.InnerException is WebException)
                        throw HttpWebClientServiceException.HttpWebClientException(url, ex.InnerException);

                    if (response == null)
                        throw HttpWebClientServiceException.Exception(url, ex);

                    if (response.StatusCode != HttpStatusCode.Redirect && response.StatusCode != HttpStatusCode.MovedPermanently)
                    {
                        throw HttpWebClientServiceException.HttpWebClientException(url, ex, response.StatusCode);
                    }
                }
                catch (WebException ex)
                {
                    // We should not get a WebException here but keep this as extra precaution
                    throw HttpWebClientServiceException.HttpWebClientException(url, ex);
                }
                catch (TaskCanceledException ex)
                {
                    // We throw a request timeout if the task gets cancelled due to the timeout setting
                    throw HttpWebClientServiceException.HttpWebClientException(url, new HttpRequestException(ex.Message),
                        HttpStatusCode.RequestTimeout);
                }
                catch (Exception ex)
                {
                    throw HttpWebClientServiceException.Exception(url, ex);
                }

                if (response.StatusCode != HttpStatusCode.Redirect && response.StatusCode != HttpStatusCode.MovedPermanently)
                {
                    return response;
                }

                // When the address has been redirected, connects to the redirection
                Uri target = response.Headers.Location;
                response.Dispose();

                if (m_redirectsRemaining-- <= 0)
                    throw HttpWebClientServiceException.RedirectsExceededException(m_url);

                m_referrer = m_url;
                m_url = new Uri(m_url, target);
                url = m_url;
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Asynchronously sends a request to the specified url.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="method">The method.</param>
        /// <param name="postData">The post data.</param>
        /// <param name="dataCompression">The data compression.</param>
        /// <param name="acceptEncoded">if set to <c>true</c> accept encoded response.</param>
        /// <param name="accept">The accept.</param>
        /// <returns></returns>
        public async Task <HttpResponseMessage> SendAsync(Uri url, HttpMethod method, HttpPostData postData,
                                                          DataCompression dataCompression,
                                                          bool acceptEncoded, string accept)
        {
            while (true)
            {
                // Store params
                m_url             = url;
                m_accept          = accept;
                m_postData        = postData;
                m_method          = postData == null || method == null ? HttpMethod.Get : method;
                m_dataCompression = postData == null ? DataCompression.None : dataCompression;
                m_acceptEncoded   = acceptEncoded;

                HttpResponseMessage response = null;
                try
                {
                    HttpClientHandler  httpClientHandler = GetHttpClientHandler();
                    HttpRequestMessage request           = GetHttpRequest();
                    response = await GetHttpResponseAsync(httpClientHandler, request).ConfigureAwait(false);

                    EnsureSuccessStatusCode(response);
                }
                catch (HttpWebClientServiceException)
                {
                    throw;
                }
                catch (HttpRequestException ex)
                {
                    if (ex.InnerException is WebException)
                    {
                        throw HttpWebClientServiceException.HttpWebClientException(url, ex.InnerException);
                    }

                    if (response == null)
                    {
                        throw HttpWebClientServiceException.Exception(url, ex);
                    }

                    if (response.StatusCode != HttpStatusCode.Redirect && response.StatusCode != HttpStatusCode.MovedPermanently)
                    {
                        throw HttpWebClientServiceException.HttpWebClientException(url, ex, response.StatusCode);
                    }
                }
                catch (WebException ex)
                {
                    // We should not get a WebException here but keep this as extra precaution
                    throw HttpWebClientServiceException.HttpWebClientException(url, ex);
                }
                catch (TaskCanceledException ex)
                {
                    // We throw a request timeout if the task gets cancelled due to the timeout setting
                    throw HttpWebClientServiceException.HttpWebClientException(url, new HttpRequestException(ex.Message),
                                                                               HttpStatusCode.RequestTimeout);
                }
                catch (Exception ex)
                {
                    throw HttpWebClientServiceException.Exception(url, ex);
                }

                if (response.StatusCode != HttpStatusCode.Redirect && response.StatusCode != HttpStatusCode.MovedPermanently)
                {
                    return(response);
                }

                // When the address has been redirected, connects to the redirection
                Uri target = response.Headers.Location;
                response.Dispose();

                if (m_redirectsRemaining-- <= 0)
                {
                    throw HttpWebClientServiceException.RedirectsExceededException(m_url);
                }

                m_referrer = m_url;
                m_url      = new Uri(m_url, target);
                url        = m_url;
            }
        }
Exemplo n.º 12
0
 /// <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));
     }
 }
Exemplo n.º 13
0
        /// <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();
                }
            }
        }