Based on the blog post by Travis Illig at http://www.paraesthesia.com/archive/2009/12/16/posting-multipartform-data-using-.net-webrequest.aspx
Ejemplo n.º 1
0
        /// <summary>
        /// Pushes a package to the server that is represented by the stream.
        /// </summary>
        /// <param name="apiKey">API key to be used to push the package.</param>
        /// <param name="packageStreamFactory">A delegate which can be used to open a stream for the package file.</param>
        /// <param name="timeout">Time in milliseconds to timeout the server request.</param>
        public void PushPackage(string apiKey, Func <Stream> packageStreamFactory, int timeout)
        {
            HttpClient client = GetClient("", "PUT", "application/octet-stream");

            client.SendingRequest += (sender, e) =>
            {
                var request = (HttpWebRequest)e.Request;

                // Set the timeout
                if (timeout <= 0)
                {
                    timeout = request.ReadWriteTimeout; // Default to 5 minutes if the value is invalid.
                }

                request.Timeout          = timeout;
                request.ReadWriteTimeout = timeout;
                if (!String.IsNullOrEmpty(apiKey))
                {
                    request.Headers.Add(ApiKeyHeader, apiKey);
                }

                var multiPartRequest = new MultipartWebRequest();
                multiPartRequest.AddFile(packageStreamFactory, "package");

                multiPartRequest.CreateMultipartRequest(request);
            };

            EnsureSuccessfulResponse(client, HttpStatusCode.Created);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Pushes a package to the server that is represented by the stream.
        /// </summary>
        /// <param name="apiKey">API key to be used to push the package.</param>
        /// <param name="packageStream">Stream representing the package.</param>
        /// <param name="timeout">Time in milliseconds to timeout the server request.</param>
        public void PushPackage(string apiKey, Stream packageStream, int timeout)
        {
            HttpClient client = GetClient("", "PUT", "application/octet-stream");

            client.SendingRequest += (sender, e) =>
            {
                var request = (HttpWebRequest)e.Request;

                // Set the timeout
                if (timeout <= 0)
                {
                    timeout = request.ReadWriteTimeout; // Default to 5 minutes if the value is invalid.
                }

                request.Timeout = timeout;
                request.ReadWriteTimeout = timeout;
                request.Headers.Add(ApiKeyHeader, apiKey);

                var multiPartRequest = new MultipartWebRequest();
                multiPartRequest.AddFile(() => packageStream, "package");

                multiPartRequest.CreateMultipartRequest(request);
            };

            EnsureSuccessfulResponse(client, HttpStatusCode.Created);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Pushes a package to the server that is represented by the stream.
        /// </summary>
        /// <param name="apiKey">API key to be used to push the package.</param>
        /// <param name="packageStreamFactory">A delegate which can be used to open a stream for the package file.</param>
        /// <param name="contentLength">Size of the package to be pushed.</param>
        /// <param name="timeout">Time in milliseconds to timeout the server request.</param>
        /// <param name="disableBuffering">Disable buffering.</param>
        private void PushPackageToServer(
            string apiKey,
            Func <Stream> packageStreamFactory,
            long packageSize,
            int timeout,
            bool disableBuffering)
        {
            int redirectionCount = 0;

            while (true)
            {
                HttpClient client = GetClient("", "PUT", "application/octet-stream");
                client.DisableBuffering = disableBuffering;

                // upload with progress
                // http://stackoverflow.com/a/20806988/18475
                // client.ProgressAvailable += (sender, args) => { args.};

                client.SendingRequest += (sender, e) =>
                {
                    SendingRequest(this, e);
                    var request = (HttpWebRequest)e.Request;

                    // Set the timeout
                    if (timeout <= 0)
                    {
                        timeout = request.ReadWriteTimeout; // Default to 5 minutes if the value is invalid.
                    }

                    request.Timeout          = timeout;
                    request.ReadWriteTimeout = timeout;
                    if (!String.IsNullOrEmpty(apiKey))
                    {
                        request.Headers.Add(ApiKeyHeader, apiKey);
                    }

                    var multiPartRequest = new MultipartWebRequest();
                    multiPartRequest.AddFile(packageStreamFactory, "package", packageSize);

                    multiPartRequest.CreateMultipartRequest(request);
                };

                // When AllowWriteStreamBuffering is set to false, redirection will not be handled
                // automatically by HttpWebRequest. So we need to check redirect status code and
                // update _baseUri and retry if redirection happens.
                if (EnsureSuccessfulResponse(client))
                {
                    return;
                }

                ++redirectionCount;
                if (redirectionCount > MaxRediretionCount)
                {
                    throw new WebException(NuGetResources.Error_TooManyRedirections);
                }
            }
        }
Ejemplo n.º 4
0
        public void PushPackage(string apiKey, Stream packageStream)
        {
            HttpClient client = GetClient("", "PUT", "application/octet-stream");

            client.SendingRequest += (sender, e) =>
            {
                var request = (HttpWebRequest)e.Request;

                // Set the timeout to the same as the read write timeout (5 mins is the default)
                request.Timeout = request.ReadWriteTimeout;
                request.Headers.Add(ApiKeyHeader, apiKey);

                var multiPartRequest = new MultipartWebRequest();
                multiPartRequest.AddFile(() => packageStream, "package");

                multiPartRequest.CreateMultipartRequest(request);
            };

            EnsureSuccessfulResponse(client);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Pushes a package to the server that is represented by the stream.
        /// </summary>
        /// <param name="apiKey">API key to be used to push the package.</param>
        /// <param name="packageStreamFactory">A delegate which can be used to open a stream for the package file.</param>
        /// <param name="contentLength">Size of the package to be pushed.</param>
        /// <param name="timeout">Time in milliseconds to timeout the server request.</param>
        private void PushPackageToServer(
            string apiKey,
            Func <Stream> packageStreamFactory,
            long packageSize,
            int timeout)
        {
            HttpClient client = GetClient("", "PUT", "application/octet-stream");

            client.SendingRequest += (sender, e) =>
            {
                SendingRequest(this, e);
                var request = (HttpWebRequest)e.Request;
                request.AllowWriteStreamBuffering = false;
                request.KeepAlive = false;

                // Set the timeout
                if (timeout <= 0)
                {
                    timeout = request.ReadWriteTimeout; // Default to 5 minutes if the value is invalid.
                }

                request.Timeout          = timeout;
                request.ReadWriteTimeout = timeout;
                if (!String.IsNullOrEmpty(apiKey))
                {
                    request.Headers.Add(ApiKeyHeader, apiKey);
                }

                var multiPartRequest = new MultipartWebRequest();
                multiPartRequest.AddFile(packageStreamFactory, "package", packageSize);

                multiPartRequest.CreateMultipartRequest(request);
            };

            EnsureSuccessfulResponse(client);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Pushes a package to the server that is represented by the stream.
        /// </summary>
        /// <param name="apiKey">API key to be used to push the package.</param>
        /// <param name="packageStreamFactory">A delegate which can be used to open a stream for the package file.</param>
        /// <param name="contentLength">Size of the package to be pushed.</param>
        /// <param name="timeout">Time in milliseconds to timeout the server request.</param>
        /// <param name="disableBuffering">Disable buffering.</param>
        private void PushPackageToServer(
            string apiKey, 
            Func<Stream> packageStreamFactory, 
            long packageSize,
            int timeout,
            bool disableBuffering) 
        {
            int redirectionCount = 0;

            while (true)
            {
                HttpClient client = GetClient("", "PUT", "application/octet-stream");
                client.DisableBuffering = disableBuffering;

                client.SendingRequest += (sender, e) =>
                {
                    SendingRequest(this, e);
                    var request = (HttpWebRequest)e.Request;
                    
                    // Set the timeout
                    if (timeout <= 0)
                    {
                        timeout = request.ReadWriteTimeout; // Default to 5 minutes if the value is invalid.
                    }

                    request.Timeout = timeout;
                    request.ReadWriteTimeout = timeout;
                    if (!String.IsNullOrEmpty(apiKey))
                    {
                        request.Headers.Add(ApiKeyHeader, apiKey);
                    }

                    var multiPartRequest = new MultipartWebRequest();
                    multiPartRequest.AddFile(packageStreamFactory, "package", packageSize);

                    multiPartRequest.CreateMultipartRequest(request);
                };

                // When AllowWriteStreamBuffering is set to false, redirection will not be handled
                // automatically by HttpWebRequest. So we need to check redirect status code and
                // update _baseUri and retry if redirection happens.
                if (EnsureSuccessfulResponse(client))
                {
                    return;
                }

                ++redirectionCount;
                if (redirectionCount > MaxRediretionCount)
                {
                    throw new WebException(NuGetResources.Error_TooManyRedirections);
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Pushes a package to the server that is represented by the stream.
        /// </summary>
        /// <param name="apiKey">API key to be used to push the package.</param>
        /// <param name="packageStreamFactory">A delegate which can be used to open a stream for the package file.</param>
        /// <param name="timeout">Time in milliseconds to timeout the server request.</param>
        private void PushPackageToServer(string apiKey, Func<Stream> packageStreamFactory, int timeout) 
        {
            HttpClient client = GetClient("", "PUT", "application/octet-stream");

            client.SendingRequest += (sender, e) =>
            {
                var request = (HttpWebRequest)e.Request;

                // Set the timeout
                if (timeout <= 0)
                {
                    timeout = request.ReadWriteTimeout; // Default to 5 minutes if the value is invalid.
                }

                request.Timeout = timeout;
                request.ReadWriteTimeout = timeout;
                if (!String.IsNullOrEmpty(apiKey))
                {
                    request.Headers.Add(ApiKeyHeader, apiKey);
                }

                var multiPartRequest = new MultipartWebRequest();
                multiPartRequest.AddFile(packageStreamFactory, "package");

                multiPartRequest.CreateMultipartRequest(request);
            };

            EnsureSuccessfulResponse(client);
        }