public static void HttpMultipart(HttpWebRequest request, Hashtable data, Hashtable files) { // We have files, so use the more complex multipart encoding. string boundary = string.Format("--{0}", DateTime.Now.Ticks.ToString("x")); request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); ArrayList items = new ArrayList(); // Determine the amount of data we will be sending long length = 0; foreach (string key in data.Keys) { PostItem item = new PostItem(key, data[key].ToString(), boundary); items.Add(item); length += item.Length; } foreach (string key in files.Keys) { PostFile file = new PostFile(key, (FileInfo)files[key], boundary); items.Add(file); length += file.Length; } length += boundary.Length + 8; request.ContentLength = length; // Now stream the data. //using (Stream requestStream = File.Create("c:\\Users\\bneely\\documents\\debug.txt")) using (Stream requestStream = request.GetRequestStream()) { foreach (PostItem item in items) { requestStream.Write(item.Header, 0, item.Header.Length); if (item.GetType() == typeof(PostFile)) { FileStream fileData = ((PostFile)item).OpenRead(); byte[] buffer = new byte[32768]; int read = 0; while ((read = fileData.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, read); } } else { byte[] itemData = UTF8Encoding.UTF8.GetBytes(item.Data); requestStream.Write(itemData, 0, itemData.Length); } byte[] end = UTF8Encoding.UTF8.GetBytes(string.Format("\r\n--{0}--\r\n", boundary)); requestStream.Write(end, 0, end.Length); } } }
public static void HttpMultipart(HttpWebRequest request, Hashtable data, Hashtable files) { // We have files, so use the more complex multipart encoding. string boundary = string.Format("--{0}", DateTime.Now.Ticks.ToString("x")); string nlBoundry = Environment.NewLine + boundary; string endBoundry = string.Format(Environment.NewLine + "{0}--" + Environment.NewLine, boundary); request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary.Replace("--", "")); ArrayList items = new ArrayList(); // Determine the amount of data we will be sending long length = 0; int x = 1; bool addNewLine = false; int addTolength = 4; foreach (string key in data.Keys) { PostItem item = new PostItem(key, data[key].ToString(), boundary); items.Add(item); length += item.Length; } foreach (string key in files.Keys) { if (addNewLine) { boundary = nlBoundry; addNewLine = false; } else { if (files.Keys.Count > 1) { addNewLine = true; } else { addTolength = 6; } } PostFile file = new PostFile(key, (FileInfo)files[key], boundary); items.Add(file); length += file.Length; } length += boundary.Length + addTolength; request.ContentLength = length; using (Stream requestStream = request.GetRequestStream()) { foreach (PostItem item in items) { requestStream.Write(item.Header, 0, item.Header.Length); if (item.GetType() == typeof(PostFile)) { FileStream fileData = ((PostFile)item).OpenRead(); byte[] buffer = new byte[32768]; int read = 0; while ((read = fileData.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, read); } } else { byte[] itemData = UTF8Encoding.UTF8.GetBytes(item.Data); requestStream.Write(itemData, 0, itemData.Length); } if (files.Keys.Count == x) { byte[] end = UTF8Encoding.UTF8.GetBytes(endBoundry); requestStream.Write(end, 0, end.Length); } x++; } } }