1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| public static WebResponse PostFile2(Uri requestUri, NameValueCollection postData, Stream fileData,Stream fileDataPj, string fileName,string fileNamePj, string fileContentType, string fileFieldName, string fileFieldNamePj, CookieContainer cookies, NameValueCollection headers)
{
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(requestUri);
string ctype;
fileContentType = string.IsNullOrEmpty(fileContentType)
? TryGetContentType(fileName, out ctype) ?
ctype : "application/octet-stream"
: fileContentType;
fileFieldName = string.IsNullOrEmpty(fileFieldName) ? "file" : fileFieldName;
fileFieldNamePj = string.IsNullOrEmpty(fileFieldNamePj) ? "file" : fileFieldNamePj;
if (headers != null)
{
// set the headers
foreach (string key in headers.AllKeys)
{
string[] values = headers.GetValues(key);
if (values != null)
foreach (string value in values)
{
webrequest.Headers.Add(key, value);
}
}
}
webrequest.Method = "POST";
webrequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1";
webrequest.KeepAlive = true;
if (cookies != null)
{
webrequest.CookieContainer = cookies;
}
string boundary = "-----------------------------" + DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture);
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
StringBuilder sbHeader = new StringBuilder();
// add form fields, if any
if (postData != null)
{
foreach (string key in postData.AllKeys)
{
string[] values = postData.GetValues(key);
if (values != null)
foreach (string value in values)
{
sbHeader.AppendFormat("--{0}\r\n", boundary);
sbHeader.AppendFormat("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n", key, value);
}
}
}
if (fileData != null)
{
sbHeader.AppendFormat("--{0}\r\n", boundary);
sbHeader.AppendFormat("Content-Disposition: form-data; name=\"{0}\"; {1}\r\n", fileFieldName, string.IsNullOrEmpty(fileName)
? "" : string.Format(CultureInfo.InvariantCulture,
"filename=\"{0}\"",Path.GetFileName(fileName)));
sbHeader.AppendFormat("Content-Type: {0}\r\n\r\n", fileContentType);
}
/// Formatage du fichier pj.zip - v 1.2
///
if (fileDataPj != null)
{
sbHeader.AppendFormat("--{0}\r\n", boundary);
sbHeader.AppendFormat("Content-Disposition: form-data; name=\"{0}\"; {1}\r\n", fileFieldNamePj, string.IsNullOrEmpty(fileNamePj)
? "" : string.Format(CultureInfo.InvariantCulture,
"filename=\"{0}\"", Path.GetFileName(fileNamePj)));
sbHeader.AppendFormat("Content-Type: {0}\r\n\r\n", fileContentType);
}
/// Fin du Formatage du fichier pj.zip - v1.2
///
byte[] header = Encoding.UTF8.GetBytes(sbHeader.ToString());
byte[] footer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
long contentLength = header.Length + (fileData != null ?
fileData.Length : 0) + footer.Length;
webrequest.ContentLength = contentLength;
using (Stream requestStream = webrequest.GetRequestStream())
{
requestStream.Write(header, 0, header.Length);
if (fileData != null)
{
// write the file data, if any
byte[] buffer = new Byte[checked((uint)Math.Min(4096,
(int)fileData.Length))];
int bytesRead;
while ((bytesRead = fileData.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
}
/// Gestion du fichier pj.zip
///
if (fileDataPj != null)
{
// write the file data, if any
byte[] bufferPj = new Byte[checked((uint)Math.Min(4096,
(int)fileDataPj.Length))];
int bytesRead;
while ((bytesRead = fileDataPj.Read(bufferPj, 0, bufferPj.Length)) != 0)
{
requestStream.Write(bufferPj, 0, bytesRead);
}
}
/// Fin de la gesiton du fichier pj.zip
///
// write footer
requestStream.Write(footer, 0, footer.Length);
return webrequest.GetResponse();
}
} |
Partager