コード例 #1
0
		async Task<WebRequestStream> CreateBody (HttpRequestMessage request, CFHTTPMessage message,
		                                         CancellationToken cancellationToken)
		{
			if (request.Content == null)
				return null;

			/*
			 * There are two ways of sending the body:
			 * 
			 * - CFHTTPMessageSetBody() sets the full body contents
			 *   We use this by default.
			 *
			 * - CFReadStreamCreateForStreamedHTTPRequest() should be used
			 *   if the body is too large to fit in memory.  It also uses
			 *   chunked transfer encoding.
			 *
			 *   We use this if the user either gave us a StreamContent, or we
			 *   don't have any Content-Length, so we'll have to use chunked
			 *   transfer anyways.
			 *
			 */
			var length = request.Content.Headers.ContentLength;
			if ((request.Content is StreamContent) || (length == null)) {
				var stream = await request.Content.ReadAsStreamAsync ().ConfigureAwait (false);
				return new WebRequestStream (stream, cancellationToken);
			}

			var text = await request.Content.ReadAsByteArrayAsync ().ConfigureAwait (false);
			message.SetBody (text);
			return null;
		}