Собсна сабж.Куда лучше пихнуть multipart/form-data параметры при пост запросе?Я вот пихал в массив но выглядит как-то не очень...
Прошу прощения за простыню кода, но я для работы с HTTP-запросами наваял маленький класс. Дарю PHP: public static class RequestBuilder { public static HttpWebRequest Get(this HttpWebRequest request) { request.Method = "GET"; return request; } public static HttpWebRequest CreateRequest(this string uri) { var request = (HttpWebRequest)WebRequest.Create(uri); return request.Timeout(60 * 1000); } public static HttpWebRequest Cookie(this HttpWebRequest request, string name, string value) { if (request.CookieContainer == null) request.CookieContainer = new CookieContainer(); if (value != null && name != null) request.CookieContainer.Add(new Cookie(name, value, string.Empty, request.Address.Host)); return request; } public static HttpWebRequest Referer(this HttpWebRequest request, string refer) { request.Referer = refer; return request; } public static HttpWebRequest UserAgent(this HttpWebRequest request, string agent) { request.UserAgent = agent; return request; } public static IHttpPostForm Post(this HttpWebRequest request, string boundary) { return new HttpPostForm(request, boundary); } public interface IHttpPostForm { IHttpPostForm Include(string name, string value); IHttpPostForm Include(string name, byte[] buffer); IHttpPostForm IncludeFile(string name, string file, string contentType, string value); IHttpPostForm IncludeFile(string name, string file, string contentType, byte[] buffer); HttpWebRequest EndForm(); } private sealed class HttpPostForm : IHttpPostForm { internal HttpPostForm(HttpWebRequest request, string boundary) { this.request = request; this.boundary = boundary; request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); } public IHttpPostForm Include(string name, string value) { builder.AppendFormat("--{0}\r\n", boundary); builder.AppendFormat("Content-Disposition: form-data; name=\"{0}\"\r\n", name); builder.Append("\r\n"); builder.Append(value); builder.Append("\r\n"); return this; } public IHttpPostForm Include(string name, byte[] buffer) { builder.AppendFormat("--{0}\r\n", boundary); builder.AppendFormat("Content-Disposition: form-data; name=\"{0}\"\r\n", name); builder.Append("\r\n"); builder.Append(buffer); builder.Append("\r\n"); return this; } public IHttpPostForm IncludeFile(string name, string file, string contentType, string value) { builder.AppendFormat("--{0}\r\n", boundary); builder.AppendFormat("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n", name, file); builder.AppendFormat("Content-Type: {0}\r\n", contentType); builder.Append("\r\n"); builder.Append(value); builder.Append("\r\n"); return this; } public IHttpPostForm IncludeFile(string name, string file, string contentType, byte[] buffer) { builder.AppendFormat("--{0}\r\n", boundary); builder.AppendFormat("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n", name, file); builder.AppendFormat("Content-Type: {0}\r\n", contentType); builder.Append("\r\n"); builder.Append(buffer); builder.Append("\r\n"); return this; } public HttpWebRequest EndForm() { builder.AppendFormat("--{0}--", boundary); request.Method = "POST"; var form = builder.ToString(); var buff = Encoding.GetEncoding("windows-1251").GetBytes(form); var stream = request.GetRequestStream(); stream.Write(buff, 0, buff.Length); stream.Close(); return request; } private readonly StringBuilder builder = new StringBuilder(); private readonly HttpWebRequest request; private readonly string boundary; } } Как им пользоваться: PHP: HttpWebRequest request = RequestBuilder.Create("http://www.google.com/") .UserAgent("Mozilla") .Referer("localhost") .Cookie("name", "value") .Post() .Include("parameter1", "value1") .Include("parameter2", "value2") .Include("parameter3", "value3") .EndForm(); Не знаю, будет ли полезно, но, по крайней мере можно посмотреть на мою реализацию
Вопрос не плохой, сам задавал его себе когда то. Мой совет: сделай функцию для отправки multipart/form-data, которая принимает один из входящих параметров некий тобою созданный класс. В своем классе реализуй метод Add, с помощью которого будешь добавлять различные данные (будь это строка какая то или даже файл). Я считаю, что это был бы хороший, юзабилити вариант.