2using System.Collections.Generic;
3using System.Collections.Specialized;
4using System.Globalization;
17 public string Name {
get;
set; }
26 var request = WebRequest.Create(address);
27 request.Method =
"POST";
28 var boundary =
"---------------------------" + DateTime.Now.Ticks.ToString(
"x", NumberFormatInfo.InvariantInfo);
29 request.ContentType =
"multipart/form-data; boundary=" + boundary;
30 boundary =
"--" + boundary;
31 using (var requestStream = request.GetRequestStream()) {
34 var boundaryBuffer = Encoding.ASCII.GetBytes(boundary +
"--");
35 requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
37 using (var response = request.GetResponse())
38 using (var responseStream = response.GetResponseStream())
39 using (var stream =
new MemoryStream()) {
40 responseStream.CopyTo(stream);
41 return stream.ToArray();
46 boundary =
"--" + boundary;
47 using (var requestStream =
new MemoryStream()) {
50 var boundaryBuffer = Encoding.ASCII.GetBytes(boundary +
"--");
51 requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
52 return requestStream.ToArray();
56 private static void WriteValues(Stream requestStream, NameValueCollection values,
string boundary) {
61 foreach (
string name
in values.Keys) {
62 var buffer = Encoding.ASCII.GetBytes(boundary +
Environment.NewLine);
63 requestStream.Write(buffer, 0, buffer.Length);
64 buffer = Encoding.ASCII.GetBytes(
string.Format(
"Content-Disposition: form-data; name=\"{0}\"{1}{1}", name,
Environment.NewLine));
65 requestStream.Write(buffer, 0, buffer.Length);
66 buffer = Encoding.UTF8.GetBytes(values[name] +
Environment.NewLine);
67 requestStream.Write(buffer, 0, buffer.Length);
76 foreach (var file
in files) {
77 var buffer = Encoding.ASCII.GetBytes(boundary +
Environment.NewLine);
78 requestStream.Write(buffer, 0, buffer.Length);
79 buffer = Encoding.UTF8.GetBytes(
string.Format(
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name, file.Filename,
Environment.NewLine));
80 requestStream.Write(buffer, 0, buffer.Length);
81 buffer = Encoding.ASCII.GetBytes(
string.Format(
"Content-Type: {0}{1}{1}", file.ContentType,
Environment.NewLine));
82 requestStream.Write(buffer, 0, buffer.Length);
83 requestStream.Write(file.Content, 0, file.Content.Length);
84 buffer = Encoding.ASCII.GetBytes(
Environment.NewLine);
85 requestStream.Write(buffer, 0, buffer.Length);
static void WriteFiles(Stream requestStream, IEnumerable< UploadFile > files, string boundary)
static void WriteValues(Stream requestStream, NameValueCollection values, string boundary)
static byte[] GetUploadFilesRequestData(IEnumerable< UploadFile > files, NameValueCollection values, string boundary)
static byte[] UploadFiles(string address, IEnumerable< UploadFile > files, NameValueCollection values)
@ Environment
Sandstorm, Hell, Above surface during Eclipse, Space