Requete HTTP POST multipart/form-data avec CF qui n'aboutie pas
Bonjour,
Je développe une application sur windows mobile pour prendre une photo et l'envoyer directement sur un serveur web via un formulaire.
Je dois donc construire une requête http POST en y joignant le fichier.
J'ai adapté une source trouvée sur le net, la requête semble bien construite mais ne réussit pas.
J'ai réussi à intercepter une requête et il semble qu'elle ne se finisse pas. (pas de boundary en fin)
Voici le code que j'utilise:
Code:
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);
} |
Voici ma requête
Code:
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
|
POST / HTTP/1.1
Connection: Keep-Alive
Expect: 100-continue
Host: 82.225.132.107
Referer: http://www.lavieestunlongfleuve.com
User-Agent: SAMSUNG-SGH-i600V/1.0 (compatible; MSIE 6.0; Windows CE; IEMobile 6.12)
Content-Length: 47282
Content-Type: multipart/form-data; boundary=-----------------------------7cf2a327f01ae
X-BlueCoat-Via: CC84AC59F19475DD
X-Forwarded-For: 10.186.122.206
X-ICAP-Version: 1.0
X-Nokia-BEARER: UMTS
X-Nokia-CONNECTION-MODE: TCP
X-Nokia-Gateway-Id: NWG/4.1/Build79
X-Nokia-Gid: 276289016239316
X-Nokia-Ipaddress: 10.186.122.206
-----------------------------7cf2a327f01ae
Content-Disposition: form-data; name="add_job"
Envoyer
-----------------------------7cf2a327f01ae
Content-Disposition: form-data; name="email"
caXXXXXXXXil.com
-----------------------------7cf2a327f01ae
Content-Disposition: form-data; name="fichierTitle"
novaduo.jpg
-----------------------------7cf2a327f01ae
Content-Disposition: form-data; name="fichier"
Content-Type: image/jpeg
ÿØÿà... |
Le fichier semble se dérouler, mais pas entièrement... et il n'y a pas de boundary en fin de requête.
Je ne vois pas pourquoi la requête ne se termine pas, elle s'interrompre durant le fichier.
Auriez-vous une idée pour me débloquer?
Charles