private static void VerifyLength(MultiPartPostPart part)
        {
            Contract.Requires(part != null);

            var expectedLength = part.Length;
            var ms = new MemoryStream();
            var sw = new StreamWriter(ms);
            part.Serialize(sw);
            sw.Flush();
            var actualLength = ms.Length;
            Assert.AreEqual(expectedLength, actualLength);
        }
        /// <summary>
        /// Creates a part that represents a simple form field.
        /// </summary>
        /// <param name="name">The name of the form field.</param>
        /// <param name="value">The value.</param>
        /// <returns>The constructed part.</returns>
        public static MultiPartPostPart CreateFormPart(string name, string value)
        {
            Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(name));
            Contract.Requires<ArgumentException>(value != null);

            var part = new MultiPartPostPart("form-data");
            part.ContentAttributes["name"] = name;
            part.Content = new MemoryStream(Encoding.UTF8.GetBytes(value));
            return part;
        }
        /// <summary>
        /// Creates a part that represents a file attachment.
        /// </summary>
        /// <param name="name">The name of the form field.</param>
        /// <param name="fileName">Name of the file as the server should see it.</param>
        /// <param name="contentType">Type of the content in HTTP Content-Type format.</param>
        /// <param name="content">The content of the file.</param>
        /// <returns>The constructed part.</returns>
        public static MultiPartPostPart CreateFormFilePart(string name, string fileName, string contentType, Stream content)
        {
            Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(name));
            Contract.Requires<ArgumentException>(fileName != null);
            Contract.Requires<ArgumentException>(contentType != null);
            Contract.Requires<ArgumentException>(content != null);

            var part = new MultiPartPostPart("form-data");
            part.ContentAttributes["name"] = name;
            part.ContentAttributes["filename"] = fileName;
            part.PartHeaders[HttpRequestHeader.ContentType] = contentType;
            if (!contentType.StartsWith("text/", StringComparison.Ordinal)) {
                part.PartHeaders["Content-Transfer-Encoding"] = "binary";
            }
            part.Content = content;
            return part;
        }