示例#1
0
        //public T Post<T>(T data) where T : class
        //{
        //    T ret = null;
        //    using (var client = new NetHttpClient())
        //    {
        //        client.SetDefaultHeaders();
        //        var content = new JsonHttpContent(data);
        //        HttpResponseMessage response = null;
        //        var t = client.PostAsync(_apiUrl, content)
        //            .ContinueWith((c) =>
        //            {
        //                c.Result.EnsureSuccessStatusCode();
        //                response = c.Result;
        //            });
        //        t.Wait(3000);

        //        ret = response.GetObject<T>();
        //    }
        //    return ret;
        //}

        public T Post <T>(T data) where T : class
        {
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_apiUrl);
            request.Method = "POST";
            request.WriteObjectAsJson(data);
            request.Accept = "application/json";
            request.Headers.Add("x-machine-name", Environment.MachineName);
            request.Headers.Add("x-site-name", HostingEnvironment.SiteName);
            System.Net.WebResponse response = request.GetResponse();
            return(response.ReadObjectByContentType <T>());
        }