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
| uri = new Uri("http://localhost/index.php?a=toto&b=titi");
string boundary = Guid.NewGuid().ToString();
HttpWebRequest request;
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = String.Format("multipart/form-data; boundary={0}", boundary);
StringBuilder content = new StringBuilder();
content.AppendLine(string.Format("--{0}", boundary));
content.AppendLine("Content-Disposition: form-data; name=\"mes_datas\"");
content.AppendLine("Content-Type: text/plain;charset=UTF-8");
content.AppendLine(Encoding.UTF8.GetString("test d'envoi de données"));
content.AppendLine(string.Format("--{0}--", boundary));
content.AppendLine();
byte[] datas = Encoding.UTF8.GetBytes(content.ToString());
request.ContentLength = datas.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(datas, 0, datas.Length);
stream.Flush();
stream.Close();
}
using (HttpWebResponse res = (HttpWebResponse)request.GetResponse())
{
if (res.StatusCode == HttpStatusCode.OK)
{
using (Stream stream = res.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, true))
{
response = reader.ReadToEnd();
reader.Close();
}
stream.Close();
}
}
else
MessageBox.Show(res.StatusDescription);
res.Close();
} |