protected virtual void ExecuteRequestWithBody(HttpWebRequest request) { request.BeginGetRequestStream(new AsyncCallback((IAsyncResult callbackResult) => { HttpWebRequest tmprequest = (HttpWebRequest)callbackResult.AsyncState; ProgressCallbackHelper copy = body.GetBody().CopyToProgress(tmprequest.EndGetRequestStream(callbackResult), null); copy.ProgressChanged += (bytesSent, totalBytes) => { body.OnProgressChange(bytesSent, totalBytes); }; copy.Completed += (totalBytes) => { body.OnCompleted(totalBytes); }; copy.Go(); // Start the asynchronous operation to get the response tmprequest.BeginGetResponse(ProcessCallback(action.Success, action.Fail), tmprequest); }), request); }
public RequestBuilder DownloadTo(String filePath) { this.success = (headers, result, HttpWebResponse) => { long?length = null; if (headers.AllKeys.Contains("Content-Length")) { length = long.Parse(headers["Content-Length"]); } FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate); ProgressCallbackHelper operation = result.CopyToProgress(fs, length); operation.Completed += (totalbytes) => { fs.Close(); }; }; return(this); }
public RequestBuilder DownloadTo(Windows.Storage.IStorageFile file) { this.success = (headers, result) => { long?length = null; if (headers.AllKeys.Contains("Content-Length")) { length = long.Parse(headers["Content-Length"]); } var handle = file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite).AsTask().Result; ProgressCallbackHelper operation = result.CopyToProgress(WindowsRuntimeStreamExtensions.AsStream(handle), length); operation.Completed += (totalbytes) => { handle.Dispose(); }; }; return(this); }
public RequestBuilder DownloadTo(String filePath, Action <long, long?> onProgressChanged, Action <WebHeaderCollection> onSuccess) { this.success = (headers, result) => { long?length = null; if (headers.AllKeys.Contains("Content-Length")) { length = long.Parse(headers["Content-Length"]); } FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate); ProgressCallbackHelper operation = result.CopyToProgress(fs, length); operation.ProgressChanged += (copied, total) => { onProgressChanged(copied, total); }; operation.Completed += (totalbytes) => { fs.Close(); onSuccess(headers); }; operation.Go(); }; return(this); }