public MultipartDataBuilder Data(MultipartData data) { this.entries.AddRange(data.Entries); return this; }
public static HttpWebResponse Execute(Uri uri, MultipartData requestData, HttpRequestMethod requestMethod, IMutableProgressTracker progress, CancellationToken cancelToken) { var requestProgress = new MutableProgressTracker(); var responseProgress = new MutableProgressTracker(); var aggregateProgress = new AggregateProgressTracker(new ProgressTrackerCollection { { requestProgress, TimeEstimates.LongOperation.ProgressWeight }, { responseProgress, TimeEstimates.ShortOperation.ProgressWeight }, }); aggregateProgress.BindTo(progress); try { var request = BuildRequest(uri, requestMethod, requestData, requestProgress, cancelToken); bool canCancel = true; cancelToken.Register(() => { if (canCancel) { request.Abort(); } }); var response = (HttpWebResponse) request.GetResponse(); responseProgress.Progress = 1; // TODO HTTP download Progress canCancel = false; return response; } catch (WebException e) { if (e.Status == WebExceptionStatus.RequestCanceled) { throw new OperationCanceledException(e.Message, e, cancelToken); } throw new OperationCanceledException(e.Message, e, cancelToken); } }
private static void WritePostData(HttpWebRequest request, MultipartData data, IMutableProgressTracker progress, CancellationToken cancelToken) { var boundary = data.Boundary; request.ContentType = string.Format(@"multipart/form-data; {0}", MultipartHeader.KeyValuePair("boundary", boundary)); request.ContentLength = Utility.GetBoundaryByteCount(boundary) + data.GetByteCount(); using (var requestStream = request.GetRequestStream()) using (var progressStream = new ProgressTrackingStreamWrapper(requestStream, request.ContentLength)) { progressStream.BindTo(progress); Utility.WriteBoundary(progressStream, boundary); data.Write(progressStream, cancelToken); } System.Diagnostics.Debug.Assert(progress.Progress == 1); }
private static HttpWebRequest BuildRequest(Uri uri, HttpRequestMethod requestMethod, MultipartData data, IMutableProgressTracker progress, CancellationToken cancelToken) { switch (requestMethod) { case HttpRequestMethod.Get: return BuildGetRequest(uri, data, progress, cancelToken); case HttpRequestMethod.Post: return BuildPostRequest(uri, data, progress, cancelToken); default: throw new ArgumentException("Unknown request method", "requestMethod"); } }
private static HttpWebRequest BuildPostRequest(Uri uri, MultipartData data, IMutableProgressTracker progress, CancellationToken cancelToken) { var request = CreateRequest(uri, @"POST"); if (data != null) { WritePostData(request, data, progress, cancelToken); } progress.Progress = 1; return request; }
private static HttpWebRequest BuildGetRequest(Uri uri, MultipartData data, IMutableProgressTracker progress, CancellationToken cancelToken) { if (data != null) { var parameters = new Dictionary<string, string>(); foreach (var entry in data.Entries) { var formEntry = entry as FormMultipartEntry; if (formEntry == null) { throw new ArgumentException("Data must contain only form entries", "data"); } parameters[formEntry.Name] = formEntry.Value; } var uriBuilder = new UriBuilder(uri) { Query = HttpUtility.ToQueryString(parameters) }; uri = uriBuilder.Uri; } var request = CreateRequest(uri, @"GET"); progress.Progress = 1; return request; }