Esempio n. 1
0
        /// <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)
        {
            Requires.NotNullOrEmpty(name, "name");
            Requires.NotNullOrEmpty(fileName, "fileName");
            Requires.NotNullOrEmpty(contentType, "contentType");
            Requires.NotNull(content, "content");

            var part = new MultipartPostPart("file");

            try {
                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);
            } catch {
                part.Dispose();
                throw;
            }
        }
Esempio n. 2
0
        /// <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);
        }
		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);
		}
Esempio n. 4
0
        /// <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)
        {
            Requires.NotNullOrEmpty(name, "name");
            Requires.NotNull(value, "value");

            var part = new MultipartPostPart("form-data");

            try {
                part.ContentAttributes["name"] = name;
                part.Content = new MemoryStream(Encoding.UTF8.GetBytes(value));
                return(part);
            } catch {
                part.Dispose();
                throw;
            }
        }
Esempio n. 5
0
        /// <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>(!string.IsNullOrEmpty(fileName));
            Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(contentType));
            Contract.Requires <ArgumentException>(content != null);

            var part = new MultipartPostPart("file");

            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);
        }
		/// <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>(!string.IsNullOrEmpty(fileName));
			Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(contentType));
			Contract.Requires<ArgumentException>(content != null);

			var part = new MultipartPostPart("file");
			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;
		}
		/// <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) {
			Requires.NotNullOrEmpty(name, "name");
			Requires.NotNullOrEmpty(fileName, "fileName");
			Requires.NotNullOrEmpty(contentType, "contentType");
			Requires.NotNull(content, "content");

			var part = new MultipartPostPart("file");
			try {
				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;
			} catch {
				part.Dispose();
				throw;
			}
		}
		/// <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) {
			Requires.NotNullOrEmpty(name, "name");
			Requires.NotNull(value, "value");

			var part = new MultipartPostPart("form-data");
			try {
				part.ContentAttributes["name"] = name;
				part.Content = new MemoryStream(Encoding.UTF8.GetBytes(value));
				return part;
			} catch {
				part.Dispose();
				throw;
			}
		}