lunes, septiembre 12, 2011

WebRequest (vs Web Service) en .Net

Los Web Services en .Net facilitan mucho el consumo de métodos web remotos vía http. Veamos una comparación entre utilizar Webservices y utilizar directamente WebRequest:

Ejemplo con WebServices:
var ws = new AEWS();
var ots = new NavOTs();
ws.Credentials = new System.Net.NetworkCredential("Administrador", "xxx", "AESQL");
Console.WriteLine("EST->{0}", ws.HayVehiculo("4552CSP"));
Ejemplo usando WebRequest:

// Create a request for the URL.
WebRequest req = WebRequest.Create("http://192.168.0.150:7047/dynamicsnav/ws/Autoequip/Codeunit/AEWS");
// If required by the server, set the credentials.
req.Credentials = new System.Net.NetworkCredential("Administrador", "xxx", "AESQL");
req.Headers.Add("SOAPAction", "\"urn:microsoft-dynamics-schemas/codeunit/AEWS:HayVehiculo\"");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.ContentLength = 350;
req.Method = "POST";
// Get the response.
string soap = @"4552CSP"; using (Stream stm = req.GetRequestStream()) {
using (StreamWriter stmw = new StreamWriter(stm)) {
stmw.Write(soap);
}
}
HttpWebResponse response1 = (HttpWebResponse)req.GetResponse();
// Display the status.
Console.WriteLine(response1.StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream1 = response1.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader1 = new StreamReader(dataStream1);
// Read the content.
string responseFromServer1 = reader1.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer1);
// Cleanup the streams and the response.
reader1.Close();
dataStream1.Close();
response1.Close();

No hay comentarios:

Publicar un comentario