HttpWebRequest envoi de fichier
Bonjour,
J'ai un petit problème en réutilisant le code trouvé dans une autre discussion , j'ai ces données a envoyer avec mon document:
USER_LOGIN
USER_PASSWD
FILE_NAME
FILE_HASH
FILE_HASH_TYPE
DOCATTACH: L’objet à verser.
RTNTYPE: Le format de réponse souhaité TXT ou XML
Le problème est que je ne vois pas quoi mettre dans cette variable sachant que je charge le document après.
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| NameValueCollection myNameValueCollection = new NameValueCollection();
//put
string uploadfile = "d:\\test\\test.txt";
myNameValueCollection.Add("USER_LOGIN","");
myNameValueCollection.Add("USER_PASSWD","");
myNameValueCollection.Add("FILE_NAME", "test");
myNameValueCollection.Add("FILE_HASH", hex);
myNameValueCollection.Add("FILE_HASH_TYPE","SHA256");
myNameValueCollection.Add("DOCATTACH", uploadfile);
myNameValueCollection.Add("RTNTYPE","XML");
string rep= Post(uri, files, myNameValueCollection); |
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
|
public static string Post(Uri url, string[] files, NameValueCollection parameters)
{
string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
string entete = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\n--" + boundary + "\r\n";
HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest2.Method = "POST";
httpWebRequest2.KeepAlive = true;
httpWebRequest2.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream memStream = new System.IO.MemoryStream();
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
memStream.Write(boundarybytes, 0, boundarybytes.Length);
string fileTemplate = "Content-Disposition: attachment; name=\"files[]\"; filename=\"{0}\";\r\n Content-Type: application/octet-stream\r\n\r\n";
byte[] fileBytes;
for (int i = 0; i < files.Length; i++)
{
if (i == 0)
{
foreach (string item in parameters)
{
byte[] headerbytes = System.Text.Encoding.ASCII.GetBytes(string.Format(entete, item, parameters[item]));
memStream.Write(headerbytes, 0, headerbytes.Length);
}
}
fileBytes = System.Text.Encoding.ASCII.GetBytes(string.Format(fileTemplate, files[i]));
memStream.Write(fileBytes, 0, fileBytes.Length);
FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
memStream.Write(boundarybytes, 0, boundarybytes.Length);
fileStream.Close();
}
httpWebRequest2.ContentLength = memStream.Length;
Stream requestStream = httpWebRequest2.GetRequestStream();
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
WebResponse webResponse2 = httpWebRequest2.GetResponse();
Stream stream2 = webResponse2.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
var result = reader2.ReadToEnd();
webResponse2.Close();
httpWebRequest2 = null;
webResponse2 = null;
return result;
} |
Pour passer le document a verser avec la variable DOCATTACH il faut que je modifie string fileTemplate ? J'ai du mal à comprendre comment cela marche :calim2:
Code:
string fileTemplate = "Content-Disposition: attachment; name=\"files[]\"; filename=\"{0}\";\r\n Content-Type: application/octet-stream\r\n\r\n";
Merci de votre aide.