ciao!
ho bisogno di fare un programma che mandi a un webserver dei dati usando il metodo post; mi sembra anche da quello che ho letto su internet che il .net non ha dei metodi specifici per usare il post, ma bisogna mandare i dati al server "manualmente" usando ad esempio questo codice
using System.Net;
...
string HttpPost (string uri, string parameters)
{
// parameters: name1=value1&name2=value2
WebRequest webRequest = WebRequest.Create (uri);
//string ProxyString =
// System.Configuration.ConfigurationManager.AppSettings
// [GetConfigKey("proxy")];
//webRequest.Proxy = new WebProxy (ProxyString, true);
//Commenting out above required change to App.Config
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes (parameters);
Stream os = null;
try
{ // send the Post
webRequest.ContentLength = bytes.Length; //Count bytes to send
os = webRequest.GetRequestStream();
os.Write (bytes, 0, bytes.Length); //Send it
}
catch (WebException ex)
{
MessageBox.Show ( ex.Message, "HttpPost: Request error",
MessageBoxButtons.OK, MessageBoxIcon.Error );
}
finally
{
if (os != null)
{
os.Close();
}
}
...
...
...
con lo stream vengono passati i byte corrispondenti ai parametri al server; la mia domanda è: i dati di un login (username e password) come devono essere formattati per poi venire trasformati in byte e essere mandati?
grazie!
|