Ejemplo n.º 1
0
        /// <summary>
        /// Asynchronously performs an HttpWebRequest with request data.
        /// </summary>
        /// <param name="request">Required. The HttpWebRequest to represent the base request.</param>
        /// <param name="contentType">Required. The Content-Type HTTP header to send with the request.</param>
        /// <param name="formData">Required. A FormBuilder object representing the form data to send.</param>
        /// <param name="cancellationToken">Optional. The token to monitor for cancellation requests.</param>
        /// <param name="progressHandler">Optional. The event handler to invoke when progress has changed.</param>
        /// <param name="completeHandler">Optional. The event handler to invoke when the request has finished.</param>
        /// <returns>Returns a HttpWebResponse representing the response from the server.</returns>
        public async Task<HttpWebResponse> RequestAsync(HttpWebRequest request, FormBuilder formData,
            CancellationToken cancellationToken = default(CancellationToken), TaskProgressEventHandler progressHandler = null, TaskCompleteEventHandler completeHandler = null)
        {
            request.ContentLength = formData.Length;

            if (!String.IsNullOrEmpty(formData.ContentType))
                request.ContentType = formData.ContentType;

            // Write to request stream
            using (Stream requestStream = await request.GetRequestStreamAsync())
            {
                await formData.WriteToAsync(requestStream, GetOptimalBufferSize(formData.Length));
            }

            HttpWebResponse result;

            {
                // HttpWebResponse: Start task
                Task<WebResponse> resultTask = request.GetResponseAsync();

                // CompleteHandler: Begin asynchronous invoke
                IAsyncResult completeHandlerResult = null;
                if (completeHandler != null)
                    completeHandler.BeginInvoke(null, null);

                // HttpWebResponse: Await result
                result = (HttpWebResponse)(await resultTask);

                // CompleteHandler: End asynchronous invoke
                if (completeHandler != null && completeHandlerResult != null)
                    completeHandler.EndInvoke(completeHandlerResult);
            }

            return result;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Performs an HttpWebRequest with request data.
        /// </summary>
        /// <param name="request">Required. The HttpWebRequest to represent the base request.</param>
        /// <param name="contentType">Required. The Content-Type HTTP header to send with the request.</param>
        /// <param name="formData">Required. A FormBuilder object representing the form data to send.</param>
        /// <returns>Returns a HttpWebResponse representing the response from the server.</returns>
        public HttpWebResponse Request(HttpWebRequest request, FormBuilder formData)
        {
            request.ContentLength = formData.Length;

            if (!String.IsNullOrEmpty(formData.ContentType))
                request.ContentType = formData.ContentType;

            // Write to upload stream
            using (Stream requestStream = request.GetRequestStream())
            {
                formData.WriteTo(requestStream, GetOptimalBufferSize(formData.Length));
            }

            return (HttpWebResponse)request.GetResponse();
        }