Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. public UsuarioServicio autenticarUsuario(UsuarioServicio usuario)
  2. {
  3. try
  4. {
  5. var address = new Uri(ConfigurationManager.AppSettings["urlServiciosRestBambu"] + "usuario/servicio");
  6. var request = WebRequest.Create(address) as HttpWebRequest;
  7.  
  8. //Método tipo POST
  9. request.Method = "POST";
  10. request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
  11.  
  12. var data = new JavaScriptSerializer().Serialize(usuario);
  13. byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
  14. request.ContentLength = byteData.Length;
  15. // Escribir los datos
  16. using (Stream postStream = request.GetRequestStream())
  17. {
  18. postStream.Write(byteData, 0, byteData.Length);
  19. }
  20.  
  21. // Obtener la respuesta
  22. StreamReader reader;
  23. string result;
  24. using (var response = request.GetResponse() as HttpWebResponse)
  25. {
  26. reader = new StreamReader(response.GetResponseStream());
  27. result = reader.ReadToEnd();
  28. }
  29.  
  30. log.Info(Resources.Mensajes.msjExitoPeticionRest);
  31. var javaScriptSerializer = new JavaScriptSerializer();
  32. UsuarioServicio usuarioAutenticado = javaScriptSerializer.Deserialize<UsuarioServicio>(result);
  33. return usuarioAutenticado;
  34. }
  35. catch (Exception excepcion)
  36. {
  37. log.Error(Resources.Mensajes.msjErrorPeticionRest, excepcion);
  38. throw excepcion;
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement