Exemple #1
0
        /// <summary>
        /// Simple method for making a request using WebHelper.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static WebHelperResponse MakeRequest(Uri url, WebHelperOptions options)
        {
            var web = new WebHelper(url);

            web.Options = options;
            return(web.MakeRequest());
        }
Exemple #2
0
        /// <summary>
        /// Simple method for making a request using WebHelper.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="options"></param>
        /// <returns>The WaitHandle that can be used to determine when the request is complete.</returns>
        public static WaitHandle MakeRequestAsync(Uri url, WebHelperOptions options)
        {
            var web = new WebHelper(url);

            web.Options = options;
            return(web.MakeRequestAsync());
        }
 /// <summary>
 /// Creates an instance of WebHelperResponse.
 /// </summary>
 /// <param name="result"></param>
 /// <param name="contentType"></param>
 /// <param name="statusCode"></param>
 /// <param name="contentEncoding"></param>
 /// <param name="charSet"></param>
 /// <param name="options"></param>
 public WebHelperResponse(object result, string contentType, HttpStatusCode statusCode, string contentEncoding, string charSet, WebHelperOptions options)
 {
     Result          = result;
     ContentType     = contentType;
     StatusCode      = statusCode;
     ContentEncoding = contentEncoding;
     CharacterSet    = charSet;
     Options         = options;
 }
Exemple #4
0
        /// <summary>
        /// Simple and performant method to download files or any large amount of content.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="fileName"></param>
        /// <param name="options"></param>
        /// <returns>The WaitHandle that can be used to determine when the request is complete.</returns>
        public static WaitHandle DownloadFileAsync(Uri url, string fileName, WebHelperOptions options = null)
        {
            var web = new WebHelper(url);

            if (options != null)
            {
                web.Options = options;
            }
            web.ProcessResponseStream += (sender, e) => { ProcessFileResponse((WebHelper)sender, e, fileName); };
            return(web.MakeRequestAsync());
        }
Exemple #5
0
 /// <summary>
 /// Simple method for making a request using WebHelper.
 /// </summary>
 /// <param name="url"></param>
 /// <param name="fileName"></param>
 /// <param name="options"></param>
 /// <returns>The WaitHandle that can be used to determine when the request is complete.</returns>
 public static WaitHandle DownloadFileAsync(string url, string fileName, WebHelperOptions options = null)
 {
     return(DownloadFileAsync(new Uri(url), fileName, options));
 }
Exemple #6
0
 /// <summary>
 /// Simple method for making a request using WebHelper.
 /// </summary>
 /// <param name="url"></param>
 /// <param name="fileName"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public static WebHelperResponse DownloadFile(string url, string fileName, WebHelperOptions options = null)
 {
     return(DownloadFile(new Uri(url), fileName, options));
 }
Exemple #7
0
 /// <summary>
 /// Simple method for making a request using WebHelper.
 /// </summary>
 /// <param name="url"></param>
 /// <param name="options"></param>
 /// <returns>The WaitHandle that can be used to determine when the request is complete.</returns>
 public static WaitHandle MakeRequestAsync(string url, WebHelperOptions options)
 {
     return(MakeRequestAsync(new Uri(url), options));
 }
Exemple #8
0
 /// <summary>
 /// Simple method for making a request using WebHelper.
 /// </summary>
 /// <param name="url"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public static WebHelperResponse MakeRequest(string url, WebHelperOptions options)
 {
     return(MakeRequest(new Uri(url), options));
 }
Exemple #9
0
 /// <summary>
 /// Creates an instance of WebHelper.
 /// </summary>
 /// <param name="url">The URL must be a valid http url.</param>
 public WebHelper(Uri url)
 {
     Url     = url;
     Options = new WebHelperOptions();
 }
Exemple #10
0
        private WebHelperResponse MakeRequest_Internal()
        {
            // Make sure we have valid options.
            if (Options == null)
            {
                Options = new WebHelperOptions();
            }

            CancellationPending = false;

            WebHelperResponse whResponse = null;

            try
            {
                HttpWebRequest request = null;
                bool           sent    = false;

                var parameters = new WebParameters(Options.Values);
                using (var contentType = ContentType ?? ContentType.CreateContentType(Options.Method, parameters))
                {
                    request = CreateRequest(contentType);
                    sent    = contentType.SendRequest(this, request);
                }

                RequestCompletedEventArgs completedArgs = null;
                if (!sent)
                {
                    if (!CancellationPending)
                    {
                        throw new InvalidOperationException("Unable to complete request. Unknown error.");
                    }
                    completedArgs = new RequestCompletedEventArgs(Options.State, true);
                }
                else
                {
                    using (var response = (HttpWebResponse)request.GetResponse())
                        whResponse = ProcessResponse(response);

                    // even if a cancellation is pending, if we recieved the response, complete the request as normal.
                    if (whResponse != null)
                    {
                        completedArgs = new RequestCompletedEventArgs(whResponse, Options.State, false);
                    }
                    else if (CancellationPending)
                    {
                        completedArgs = new RequestCompletedEventArgs(whResponse, Options.State, true);
                    }
                    else
                    {
                        throw new InvalidOperationException("Unable to receive response. Unknown error.");
                    }
                }

                Post((arg) =>
                {
                    OnRequestCompleted((RequestCompletedEventArgs)arg);
                }, completedArgs);
            }
            catch (Exception ex)
            {
                var completedArgs = new RequestCompletedEventArgs(ex, Options.State, false);
                Post((arg) =>
                {
                    // Regardless of the outcome, RequestCompleted is guaranteed to be raised.
                    // Caller is responsible for checking the status of the request object to
                    // see if it is successful or not.
                    OnRequestCompleted((RequestCompletedEventArgs)arg);
                }, completedArgs);

                if (mAsyncOperation == null)
                {
                    // Don't throw the exception if running on a thread. It can't be caught
                    // and makes it difficult to debug (cannot continue in Visual Studio).
                    throw;
                }
            }
            finally
            {
                if (mAsyncOperation != null)
                {
                    using (var done = new ManualResetEvent(false))
                    {
                        // Use the ManualResetEvent to ensure that the operation completes before
                        // we exit the method.
                        // This is used to ensure that the Wait method will not continue until
                        // after the final operation has been completed.
                        mAsyncOperation.PostOperationCompleted((arg) =>
                        {
                            mAsyncOperation = null;
                            done.Set();
                        }, null);
                        done.WaitOne();
                    }
                }
            }
            return(whResponse);
        }