switch (contentType) { case "application/x-www-form-urlencoded": if (parameters.Any(param => param.IsFile)) throw new ArgumentException("application/x-www-form-urlencodedでファイルを送ることはできません。"); req.ContentType = "application/x-www-form-urlencoded"; using (var sw = new StreamWriter(req.GetRequestStream())) sw.Write(string.Join("&", parameters.Select(data => Uri.EscapeDataString(data.Name) + "=" + Uri.EscapeDataString(data.Content)))); break; case "multipart/form-data": var boundary = Guid.NewGuid().ToString(); req.ContentType = "multipart/form-data; boundary=" + boundary; using (var stream = req.GetRequestStream()) { foreach (var param in parameters) { stream.WriteText("--" + boundary + "\r\n"); var contentDisposition = new List<string>(); contentDisposition.Add("form-data"); contentDisposition.Add(string.Format(@"name=""{0}""", param.Name)); if (param.IsFile) contentDisposition.Add(string.Format(@"filename=""{0}""", Path.GetFileName(param.Content))); stream.WriteText("Content-Disposition: " + string.Join("; ", contentDisposition)); if (!string.IsNullOrEmpty(param.ContentType)) stream.WriteText("Content-Type: " + param.ContentType); stream.WriteText("\r\n\r\n"); if (!param.IsFile) { stream.WriteText(param.Content); } else { stream.WriteAll(File.ReadAllBytes(param.Content)); } stream.WriteText("\r\n"); } stream.WriteText("--" + boundary + "--"); } break; }