public static Tuple <string, byte[]> SerializeData(this XElement form, System.Text.Encoding encoding = null) { var values = form.Serialize(); var action = (string)form.Attribute("action"); var multipart = "multipart/form-data".Is((string)form.Attribute("enctype")); var boundary = multipart ? ("---xhtmlr-" + Guid.NewGuid().ToString()) : null; var ct = multipart ? ("multipart/form-data, boundary=" + boundary) : "application/x-www-form-urlencoded"; using (var mem = new System.IO.MemoryStream()) { if (multipart) { foreach (var key in values) { mem.WriteLine("--" + boundary, encoding); mem.WriteLine("Content-Disposition: form-data; name=\"" + key.Name + "\"", encoding); mem.WriteLine(); var value = key.Value; if (value is FilePointer ptr) { if (!ptr.File.IsNullOrEmpty()) { value = System.IO.File.ReadAllBytes(ptr.File); } } else if (value is byte[]) { mem.Write((byte[])value); } else { mem.WriteLine(Convert.ToString(value), encoding); } } mem.WriteLine("--" + boundary + "--", encoding); } else { var sep = string.Empty; foreach (var key in values) { mem.Write(sep + Uri.EscapeDataString(key.Name) + "=" + Uri.EscapeDataString(Convert.ToString(key.Value)), encoding); sep = "&"; } } return(Tuple.Create(ct, mem.ToArray())); } }
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]; // Set the boundary ms.WriteLine("--" + Boundary); // Set 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)); // Set 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; // Set the RawData of our request return(CachedData = ms.ToArray()); } }
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]; // Set the boundary ms.WriteLine("--" + Boundary); // Set 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)); // Set 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; // Set the RawData of our request return CachedData = ms.ToArray(); } }