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
|
try
{
//Envoi de la requete
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://www.novaduo.com/");
// Set the 'Method' property of the 'Webrequest' to 'POST'.
myReq.Method = "POST";
myReq.UserAgent = this.cUserAgent;
myReq.Referer = "http://www.lavieestunlongfleuve.com";
myReq.ContentType = "multipart/form-data; boundary=" + this.cMultiPartBoundary;
string postData = "";
postData = cMultiPartBoundary + "\r\n";
postData += "Content-Disposition: form-data; name=\"add_job\"" + "\r\n";
postData += "Envoyer" + "\r\n" + cMultiPartBoundary + "\r\n";
postData += "Content-Disposition: form-data; name=\"email\"" + "\r\n" + "\r\n";
postData += "canssens@gmail.com\r\n" + cMultiPartBoundary + "\r\n";
postData += "Content-Disposition: form-data; name=\"fichier\"; filename=\"photo.jpg\"" + "\r\n";
postData += "Content-Type: image/jpeg\r\n" + "\r\n";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
//i then attach the file to be uploaded to this byte array
byte[] byte2 = new byte[4096];
int BlockSize;
MemoryStream TempStream = new MemoryStream();
FileStream myFileStream = new FileStream(@"\Storage Card\My Pictures\novaduo.jpg", FileMode.Open); //
do
{
BlockSize = myFileStream.Read(byte2, 0, 4096);
if (BlockSize > 0) TempStream.Write(byte2, 0, BlockSize);
} while (BlockSize > 0);
byte[] data2 = TempStream.ToArray();
//I then join the two byte arrays together and close the Http POST request byte array
postData = "\r\n" + cMultiPartBoundary + "\r\n";
byte[] data3 = encoding.GetBytes(postData);
//return the complete binary data
byte[] dataTotal = new Byte[data.Length + data2.Length + data3.Length];
data.CopyTo(dataTotal, 0);
data2.CopyTo(dataTotal, data.Length);
data3.CopyTo(dataTotal, (data.Length + data2.Length));
//The request NOW
myReq.ContentLength = dataTotal.Length;
Stream myStream = myReq.GetRequestStream();
myStream.Write(dataTotal, 0, dataTotal.Length);
myStream.Close();
MessageBox.Show("done", this.Text);
}
catch (Exception e2)
{
MessageBox.Show("Erreur " + e2, this.Text);
Console.WriteLine("{0} Exception caught.", e2);
} |
Partager