Impossible d'effectuer une requete HTTP POST avec HttpWebRequest
Salut,
J'essaye d'effectuer une requete Http POST en multipart/form-data pour simuler l'envoi d'un fichier mais la requete ne semble pas s'effectuer en entier (je n'ai que les headers de la requete HTTP) :
Citation:
[root@localhost rapha]# python server.py
IP : ('127.0.0.1', 32851)
POST / HTTP/1.1
User-Agent: FilesApi/FilesAPI v0.1 (Unix 2.6.27.7)
Referer:
http://api.files.getwebb.org
Content-Type: multipart/form-data; boundary=-------------------906641065
Content-Length: 1845
Expect: 100-continue
Connection: keep-alive
Host: 127.0.0.1
Alors que normalement, je devrais avoir quelque chose comme ceci :
Citation:
[root@localhost rapha]# python server.py
IP : ('127.0.0.1', 32851)
POST / HTTP/1.1
User-Agent: FilesApi/FilesAPI v0.1 (Unix 2.6.27.7)
Referer:
http://api.files.getwebb.org
Content-Type: multipart/form-data; boundary=-------------------906641065
Content-Length: 1845
Expect: 100-continue
Connection: keep-alive
Host: 127.0.0.1
-------------------906641065
Content-Disposition: form-data; name="fichier1"; filename="icon.png"
Content-Type: application/octet-stream
(contenu de l'image)
---------------------906641065Content-Disposition: form-data; name="nb_fichiers"
1
---------------------906641065
Content-Disposition: form-data; name="votes"
on
---------------------906641065--
Voici mon code :
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
| string Boundary = "-------------------" + (new System.Random()).Next(); // Séparateur des éléments du formulaire
string Data = Boundary + "\r\n";
// Création de la requête pour les fichiers
int FichierNo = 0;
foreach(string Fichier in this.FilesList) {
FichierNo++;
System.IO.FileInfo FileInfos = new System.IO.FileInfo(Fichier);
Data += "Content-Disposition: form-data; name=\"fichier" + FichierNo + "\"; filename=\"" + FileInfos.Name + "\"\r\n"; // Nom du fichier
Data += "Content-Type: application/octet-stream\r\n\r\n"; // Mimetype
Data += (new System.IO.StreamReader(Fichier)).ReadToEnd()+ "\r\n"; // Ajout du contenu du fichier
Data += "--" + Boundary; // Fin de l'enregistrement
}
// Nombre de fichiers
Data += "Content-Disposition: form-data; name=\"nb_fichiers\"\r\n\r\n";
Data += FichierNo + "\r\n";
Data += "--" + Boundary;
// Activer le vote
if(this.EnableVotes) {
Data += "\r\nContent-Disposition: form-data; name=\"votes\"\r\n\r\n";
Data += "on\r\n";
Data += "--" + Boundary;
}
// Envoyer par email
if(this.Email != null) {
Data += "\r\nContent-Disposition: form-data; name=\"check_email\"\r\n\r\n";
Data += "on\r\n";
Data += "--" + Boundary + "\r\n";
Data += "Content-Disposition: form-data; name=\"email\"\r\n\r\n";
Data += this.Email + "\r\n";
Data += "--" + Boundary;
}
// Fin de requete
Data += "--\r\n";
byte[] DataBytes = (new System.Text.ASCIIEncoding()).GetBytes(Data);
// Envoi de la requete
System.Net.HttpWebRequest Request = (System.Net.HttpWebRequest) System.Net.WebRequest.Create("http://127.0.0.1/");
Request.Method = "POST";
Request.UserAgent = this.Agent;
Request.Referer = "http://api.files.getwebb.org";
Request.ContentType = "multipart/form-data; boundary=" + Boundary;
Request.ContentLength = DataBytes.Length;
// Envoi des données
System.IO.Stream PostData = Request.GetRequestStream();
PostData.Write(DataBytes, 0, DataBytes.Length);
PostData.Close(); |
Je ne comprends vraiment pas, parce que meme le code du MSDN ne marche pas :? : http://msdn.microsoft.com/fr-fr/libr...od(VS.80).aspx
Il faut faire quelque chose en plus et j'aurais loupé ca ?
Note : Mon code pour intercepter la requete en Python marche avec un navigateur, donc ca ne peut pas etre ca le problème :roll:.
Merci