Example #1
0
 /// <summary>
 /// Creates an instance of WebHelperOptions.
 /// </summary>
 public WebHelperOptions()
 {
     Headers           = new WebHeaderCollection();
     ResponseEncoding  = Encoding.Default;
     BufferSize        = 1024;
     Values            = new WebParameters();
     Timeout           = TimeSpan.FromSeconds(100);
     Method            = HttpMethod.Get;
     UserAgent         = "";
     KeepAlive         = true;
     AllowAutoRedirect = true;
     UseCompression    = true;
 }
Example #2
0
 /// <summary>
 /// Creates a new instance of a ContentType based on the parameters sent in.
 /// </summary>
 /// <param name="method"></param>
 /// <param name="parameters"></param>
 /// <returns></returns>
 public static ContentType CreateContentType(HttpMethod method, WebParameters parameters)
 {
     if (parameters.Files.Count > 0 || parameters.Binary.Count > 0)
     {
         return(new MultipartFormDataContentType(parameters));
     }
     else if (parameters.Count > 0 && method != HttpMethod.Get && method != HttpMethod.Delete)
     {
         return(new ApplicationUrlEncodedContentType(parameters));
     }
     else
     {
         return(new NoContentType(parameters));
     }
 }
Example #3
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);
        }
Example #4
0
 /// <summary>
 /// Creates an instance of MultipartFormDataContentType.
 /// </summary>
 public MultipartFormDataContentType(WebParameters parameters)
     : base(parameters)
 {
     PartBoundary = "---------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
 }
Example #5
0
 /// <summary>
 /// Creates an instance of ContentType.
 /// </summary>
 /// <param name="parameters"></param>
 protected ContentType(WebParameters parameters)
 {
     Parameters = parameters;
 }
Example #6
0
 /// <summary>
 /// Creates an instance of ApplicationUrlEncodedContentType.
 /// </summary>
 /// <param name="parameters"></param>
 public ApplicationUrlEncodedContentType(WebParameters parameters)
     : base(parameters)
 {
 }
Example #7
0
 /// <summary>
 /// Creates an instance of NoContentType.
 /// </summary>
 /// <param name="parameters"></param>
 public NoContentType(WebParameters parameters)
     : base(parameters)
 {
 }