private T Execute <T>(string method, IVm vm) where T : IVm, new() { string url = String.Format("{0}/{1}", _host, method); var data = JsonConvert.SerializeObject(vm); var request = WebRequest.Create(url); request.ContentType = "application/json"; request.Method = "POST"; request.ContentLength = data.Length; request.Headers.Add(AuthorizationHeader, _securityProvider.GetToken()); StreamWriter requestWriter = new StreamWriter(request.GetRequestStream()); requestWriter.Write(data); requestWriter.Close(); using (var response = request.GetResponse() as HttpWebResponse) { if (response == null || response.StatusCode != HttpStatusCode.OK) { return(new T()); } using (var stream = response.GetResponseStream()) { if (stream == null) { return(new T()); } using (var reader = new StreamReader(stream)) { return(JsonConvert.DeserializeObject <T>(reader.ReadToEnd())); } } } }