12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using BestHTTP.Extensions;
- namespace BestHTTP.Forms
- {
- /// <summary>
- /// A HTTP Form implementation to send textual and binary values.
- /// </summary>
- public sealed class HTTPMultiPartForm : HTTPFormBase
- {
- #region Private Fields
- /// <summary>
- /// A random boundary generated in the constructor.
- /// </summary>
- private string Boundary;
- /// <summary>
- ///
- /// </summary>
- 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
- };
- }
|