using BestHTTP.Extensions; namespace BestHTTP.Forms { /// /// A HTTP Form implementation to send textual and binary values. /// public sealed class HTTPMultiPartForm : HTTPFormBase { #region Private Fields /// /// A random boundary generated in the constructor. /// private string Boundary; /// /// /// private byte[] CachedData; #endregion public HTTPMultiPartForm() { this.Boundary = "BestHTTP_HTTPMultiPartForm_" + this.GetHashCode().ToString("X"); } #region IHTTPForm Implementation public override void PrepareRequest(HTTPRequest request) { // SaveLocal up Content-Type header for the request request.SetHeader("Content-Type", "multipart/form-data; boundary=\"" + Boundary + "\""); } public override byte[] GetData() { if (CachedData != null) return CachedData; using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { for (int i = 0; i < Fields.Count; ++i) { HTTPFieldData field = Fields[i]; // SaveLocal the boundary ms.WriteLine("--" + Boundary); // SaveLocal up Content-Disposition header to our form with the name ms.WriteLine("Content-Disposition: form-data; name=\"" + field.Name + "\"" + (!string.IsNullOrEmpty(field.FileName) ? "; filename=\"" + field.FileName + "\"" : string.Empty)); // SaveLocal up Content-Type head for the form. if (!string.IsNullOrEmpty(field.MimeType)) ms.WriteLine("Content-Type: " + field.MimeType); ms.WriteLine("Content-Length: " + field.Payload.Length.ToString()); ms.WriteLine(); // Write the actual data to the MemoryStream ms.Write(field.Payload, 0, field.Payload.Length); ms.Write(HTTPRequest.EOL, 0, HTTPRequest.EOL.Length); } // Write out the trailing boundary ms.WriteLine("--" + Boundary + "--"); IsChanged = false; // SaveLocal the RawData of our request return CachedData = ms.ToArray(); } } #endregion }; }