12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using BestHTTP.Extensions;
- namespace BestHTTP.Forms
- {
-
-
-
- public sealed class HTTPMultiPartForm : HTTPFormBase
- {
- #region Private Fields
-
-
-
- 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)
- {
-
- 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];
-
- ms.WriteLine("--" + Boundary);
-
- ms.WriteLine("Content-Disposition: form-data; name=\"" + field.Name + "\"" + (!string.IsNullOrEmpty(field.FileName) ? "; filename=\"" + field.FileName + "\"" : string.Empty));
-
- if (!string.IsNullOrEmpty(field.MimeType))
- ms.WriteLine("Content-Type: " + field.MimeType);
- ms.WriteLine("Content-Length: " + field.Payload.Length.ToString());
- ms.WriteLine();
-
- ms.Write(field.Payload, 0, field.Payload.Length);
- ms.Write(HTTPRequest.EOL, 0, HTTPRequest.EOL.Length);
- }
-
- ms.WriteLine("--" + Boundary + "--");
- IsChanged = false;
-
- return CachedData = ms.ToArray();
- }
- }
- #endregion
- };
- }
|