public void UploadDataAsync(Uri uri, string method, byte[] bytes)
        {
            var client = new AccurateWebClient(bytes.Length);

            SetupClient(client);

            PerformRetryableRequest("HTTP " + method + " " + uri, uri, u => client.UploadDataAsync(u, method, bytes));
        }
示例#2
0
        private void PushBinary(string targetUri, byte[] bytes)
        {
            try
            {
                using (var client = new AccurateWebClient(bytes.Length))
                {
                    Console.WriteLine("HTTP PUT " + targetUri);
                    var       done   = false;
                    byte[]    result = null;
                    Exception ex     = null;
                    var       uploadProgressRenderer = new UploadProgressRenderer();
                    client.UploadDataCompleted += (sender, e) => {
                        if (e.Error != null)
                        {
                            ex = e.Error;
                        }

                        result = e.Result;
                        done   = true;
                    };
                    client.UploadProgressChanged += (sender, e) => {
                        if (!done)
                        {
                            uploadProgressRenderer.Update(e.ProgressPercentage, e.BytesSent / 1024);
                        }
                    };
                    client.UploadDataAsync(new Uri(targetUri), "PUT", bytes);
                    while (!done)
                    {
                        System.Threading.Thread.Sleep(0);
                    }

                    uploadProgressRenderer.Finalize();

                    if (ex != null)
                    {
                        throw new InvalidOperationException("Upload error", ex);
                    }

                    return;
                }
            }
            catch (WebException)
            {
                if (targetUri.StartsWith("https://"))
                {
                    // Attempt fallback to HTTP.
                    Console.Error.WriteLine("Web exception while using HTTPS; attempting HTTP fallback...");
                    targetUri = "http" + targetUri.Substring("https".Length);
                    PushBinary(targetUri, bytes);
                    return;
                }

                Console.WriteLine("Web exception when sending to: " + targetUri);
                throw;
            }
        }
        private void PushBinary(string targetUri, string file)
        {
            byte[] bytes;
            using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                bytes = new byte[(int)stream.Length];
                stream.Read(bytes, 0, bytes.Length);
            }

            try
            {
                using (var client = new AccurateWebClient(bytes.Length))
                {
                    Console.WriteLine("HTTP PUT " + targetUri);
                    var       done   = false;
                    byte[]    result = null;
                    Exception ex     = null;
                    var       uploadProgressRenderer = new UploadProgressRenderer();
                    client.UploadDataCompleted += (sender, e) => {
                        if (e.Error != null)
                        {
                            ex = e.Error;
                        }

                        result = e.Result;
                        done   = true;
                    };
                    client.UploadProgressChanged += (sender, e) => {
                        if (!done)
                        {
                            uploadProgressRenderer.Update(e.ProgressPercentage, e.BytesSent / 1024);
                        }
                    };
                    client.UploadDataAsync(new Uri(targetUri), "PUT", bytes);
                    while (!done)
                    {
                        System.Threading.Thread.Sleep(0);
                    }

                    Console.WriteLine();

                    if (ex != null)
                    {
                        throw new InvalidOperationException("Upload error", ex);
                    }

                    return;
                }
            }
            catch (WebException)
            {
                Console.WriteLine("Web exception when sending to: " + targetUri);
                throw;
            }
        }
        private void PushBinary(string targetUri, string file)
        {
            byte[] bytes;
            using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                bytes = new byte[(int)stream.Length];
                stream.Read(bytes, 0, bytes.Length);
            }

            try
            {
                using (var client = new AccurateWebClient(bytes.Length))
                {
                    Console.WriteLine("HTTP PUT " + targetUri);
                    var done = false;
                    byte[] result = null;
                    Exception ex = null;
                    var uploadProgressRenderer = new UploadProgressRenderer();
                    client.UploadDataCompleted += (sender, e) => {
                        if (e.Error != null)
                        {
                            ex = e.Error;
                        }

                        result = e.Result;
                        done = true;
                    };
                    client.UploadProgressChanged += (sender, e) => {
                        if (!done)
                        {
                            uploadProgressRenderer.Update(e.ProgressPercentage, e.BytesSent / 1024);
                        }
                    };
                    client.UploadDataAsync(new Uri(targetUri), "PUT", bytes);
                    while (!done)
                    {
                        System.Threading.Thread.Sleep(0);
                    }

                    uploadProgressRenderer.Finalize();

                    if (ex != null)
                    {
                        throw new InvalidOperationException("Upload error", ex);
                    }

                    return;
                }
            }
            catch (WebException)
            {
                if (targetUri.StartsWith("https://"))
                {
                    // Attempt fallback to HTTP.
                    Console.Error.WriteLine("Web exception while using HTTPS; attempting HTTP fallback...");
                    targetUri = "http" + targetUri.Substring("https".Length);
                    PushBinary(targetUri, file);
                    return;
                }

                Console.WriteLine("Web exception when sending to: " + targetUri);
                throw;
            }
        }
        private void PushBinary(string targetUri, byte[] bytes)
        {
            try
            {
                using (var client = new AccurateWebClient(bytes.Length))
                {
                    Console.WriteLine("HTTP PUT " + targetUri);
                    var done = false;
                    byte[] result = null;
                    Exception ex = null;
                    var uploadProgressRenderer = new UploadProgressRenderer();
                    client.UploadDataCompleted += (sender, e) => {
                        if (e.Error != null)
                        {
                            ex = e.Error;
                        }

                        result = e.Result;
                        done = true;
                    };
                    client.UploadProgressChanged += (sender, e) => {
                        if (!done)
                        {
                            uploadProgressRenderer.Update(e.ProgressPercentage, e.BytesSent / 1024);
                        }
                    };
                    client.UploadDataAsync(new Uri(targetUri), "PUT", bytes);
                    while (!done)
                    {
                        System.Threading.Thread.Sleep(0);
                    }

                    uploadProgressRenderer.Finalize();

                    if (ex != null)
                    {
                        throw new InvalidOperationException("Upload error", ex);
                    }

                    return;
                }
            }
            catch (WebException)
            {
                Console.WriteLine("Web exception when sending to: " + targetUri);
                throw;
            }
        }