J'imagine qu'une variable en session sera plus sécurisée alors.
Tiens, je te prête ce bout de 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
| public static string PostRequest(Uri uri, string text, bool waitResponse)
{
byte[] bits = Encoding.UTF8.GetBytes(text);
byte[] buffer = new byte[2048];
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(uri);
httpReq.Method = "POST";
httpReq.ContentLength = bits.Length;
httpReq.ContentType = "application/x-www-form-urlencoded";
using (MemoryStream ms = new MemoryStream(bits)) // Put data in memory
{
using (Stream stream = httpReq.GetRequestStream()) // Open request stream
{
int block;
while ((block = ms.Read(buffer, 0, buffer.Length)) > 0) // Read a memory chunck of buffer size. Write it to the buffer.
{
stream.Write(buffer, 0, block); // Write buffer to stream
}
}
}
if (waitResponse)
{
HttpWebResponse resp = (HttpWebResponse)httpReq.GetResponse();
using (Stream stream = resp.GetResponseStream())
{
using (StreamReader sr = new StreamReader(stream))
{
return sr.ReadToEnd();
}
}
}
else
{
return null;
}
} |
En faisant
PostRequest(new Uri("http://localhost:51920/MonWebServices.asmx/HelloWorld"), "hello=Immobilis", true)
Sur un Web service comme
1 2 3 4 5
| [WebMethod(Description="Hello parameter")]
public string HelloWorld(string hello)
{
return string.Format("Hello {0}", hello);
} |
J'obtiens
1 2
| <?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.monsite.com/">Hello Immobilis</string> |
A+
Partager