Exemplo n.º 1
0
        private void PrepareWebRequestForContentUpload(HttpWebRequest webRequest, HttpRequestMessage request)
        {
            HttpContent requestContent = request.Content;

            if (requestContent == null)
            {
                // If we have no content, we set the Content-Length to 0, regardless of the Transfer-Encoding header.
                webRequest.ContentLength = 0;
            }
            else
            {
                MediaTypeHeaderValue contentType = requestContent.Headers.ContentType;
                if (contentType != null)
                {
                    webRequest.ContentType = contentType.ToString();
                }

                // Determine how to communicate the length of the request content.
                if (request.Headers.TransferEncodingChunked == true)
                {
                    webRequest.SendChunked = true;
                }
                else
                {
                    long?contentLength = requestContent.Headers.ContentLength;
                    if (contentLength != null)
                    {
                        webRequest.ContentLength = (long)contentLength;
                    }
                    else
                    {
                        // If we don't have a content length and we don't use chunked, then we must buffer the content.
                        // If the user specified a zero buffer size, we throw.
                        if (maxRequestContentBufferSize == 0)
                        {
                            throw new HttpException(
                                      "The content length of the request content can't be determined. Either set TransferEncodingChunked to true, load content into buffer, or set AllowRequestContentBuffering to true.");
                        }

                        // HttpContent couldn't calculate the content length. Chunked is not specified. Buffer the
                        // content to get the content length.
                        // Note that we call LoadIntoBuffer() synchronously since the scenario where neither the length
                        // is known nor chunked is used, often means that we're about to serialize in-memory data like
                        // XML, objects, feed content, etc. I.e. loading sync results in better perf. than async.
                        // If customers want to use async buffering (e.g. the source is a network stream with
                        // unknown length), LoadIntoBufferAsync() can be called before sending the request. In this
                        // case the length is known and we'll not end up here.
                        requestContent.LoadIntoBuffer(maxRequestContentBufferSize);
                        contentLength = requestContent.Headers.ContentLength;
                        Contract.Assert(contentLength != null, "After buffering content, ContentLength must not be null.");
                        webRequest.ContentLength = (long)contentLength;
                    }
                }
            }
        }