Exemple #1
0
        /// <summary>
        /// 提交内容
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static T Post <T>(string url, object data)
        {
            var value = string.Empty;

            if (data != null)
            {
                value = Newtonsoft.Json.JsonConvert.SerializeObject(data);
            }
            string content;

            using (var client = new HttpClient())
            {
                var response = client.PostAsync(new Uri(url), new StringContent(value, Encoding.UTF8)).Result;
                content = response.Content.ReadAsStringAsync().Result;
            }
            if (content.IndexOf("\"errcode\"") > -1)
            {
                var error = Newtonsoft.Json.JsonConvert.DeserializeObject <Data.Error>(content);
                if (error.errcode != 0)
                {
                    throw CallException.Create(error);
                }
            }
            return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(content));
        }
Exemple #2
0
        public static T Get <T>(string url)
        {
            string content;

            using (var client = new HttpClient())
            {
                var response = client.GetAsync(new Uri(url)).Result;
                content = response.Content.ReadAsStringAsync().Result;
            }
            if (content.IndexOf("\"errcode\"") > -1)
            {
                var error = Newtonsoft.Json.JsonConvert.DeserializeObject <Data.Error>(content);
                if (error.errcode != 0)
                {
                    throw CallException.Create(error);
                }
            }
            return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(content));
        }