/// <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="data">Required. A byte array representing the request data to send with the request.</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, string contentType, byte[] data, CancellationToken cancellationToken = default(CancellationToken), TaskProgressEventHandler progressHandler = null, TaskCompleteEventHandler completeHandler = null) { request.ContentLength = (data != null) ? data.Length : 0; if (!String.IsNullOrEmpty(contentType)) request.ContentType = contentType; // Write to request stream using (Stream requestStream = await request.GetRequestStreamAsync()) { int bufferSize = GetOptimalBufferSize(data.Length); int bytesDone = new int(); while (bytesDone < data.Length) { // ProgressHandler: Begin asynchronous invoke IAsyncResult progressHandlerResult = null; if (progressHandler != null) progressHandlerResult = progressHandler.BeginInvoke(Math.Min(1.0, (double)bytesDone / (double)data.Length), null, null); // WriteTask: Start writing to stream asynchronously int nextChunkSize = Math.Min(data.Length - bytesDone, bufferSize); Task writeTask = requestStream.WriteAsync(data, bytesDone, nextChunkSize, cancellationToken); // End asynchronous ProgressHandler if (progressHandler != null && progressHandlerResult != null) progressHandler.EndInvoke(progressHandlerResult); // WriteTask: Wait for chunk upload to finish await writeTask; bytesDone += nextChunkSize; } } 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; }
/// <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; }