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
| private void button4_Click(object sender, EventArgs e)
{
string url_upload_photos = "http://monsite.xxxx";
int nbr_photos = 0;
string[] files_photos = { "C:/1.jpg", "C:/2.jpg" };
string entete_fichier = "";
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture);
HttpWebRequest httpWebRequest_upload = (HttpWebRequest)WebRequest.Create(url_upload_photos);
httpWebRequest_upload.Method = "POST";
httpWebRequest_upload.KeepAlive = true;
httpWebRequest_upload.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
httpWebRequest_upload.Referer = "http://monsite.xxxx";
httpWebRequest_upload.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; FDM; .NET4.0C; .NET4.0E)";
httpWebRequest_upload.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest_upload.ServicePoint.Expect100Continue = false;
httpWebRequest_upload.CookieContainer = cookies;
Stream memStream_upload = new System.IO.MemoryStream();
string entete = "";
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
memStream_upload.Write(boundarybytes, 0, boundarybytes.Length);
byte[] headerbytes = System.Text.Encoding.ASCII.GetBytes(entete);
memStream_upload.Write(headerbytes, 0, headerbytes.Length);
entete_fichier = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: image/jpeg\r\n\r\n";
entete_fichier = string.Format(entete_fichier, "pictureUpload1", files_photos[nbr_photos]);
FileStream fileStream_upload = new FileStream(files_photos[nbr_photos], FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;
byte[] headerbytes_0 = System.Text.Encoding.ASCII.GetBytes(entete_fichier);
memStream_upload.Write(headerbytes_0, 0, headerbytes_0.Length);
while ((bytesRead = fileStream_upload.Read(buffer, 0, buffer.Length)) != 0)
{
memStream_upload.Write(buffer, 0, bytesRead);
}
memStream_upload.Write(boundarybytes, 0, boundarybytes.Length);
string entete_suite = "";
byte[] headerbytes_suite = System.Text.Encoding.ASCII.GetBytes(entete_suite);
memStream_upload.Write(headerbytes_suite, 0, headerbytes_suite.Length);
fileStream_upload.Close();
nbr_photos++;
httpWebRequest_upload.ContentLength = memStream_upload.Length;
Stream requestStream_upload = httpWebRequest_upload.GetRequestStream();
memStream_upload.Position = 0;
byte[] tempBuffer = new byte[memStream_upload.Length];
memStream_upload.Read(tempBuffer, 0, tempBuffer.Length);
memStream_upload.Close();
requestStream_upload.Write(tempBuffer, 0, tempBuffer.Length);
requestStream_upload.Close();
WebResponse webResponse_upload = httpWebRequest_upload.GetResponse();
Stream stream_upload = webResponse_upload.GetResponseStream();
StreamReader reader_upload = new StreamReader(stream_upload);
string resultat = reader_upload.ReadToEnd();
webResponse_upload.Close();
httpWebRequest_upload = null;
webResponse_upload = null;
} |
Partager