Пример #1
0
        public static HttpResponseResult ConvertToResult(this HttpResponseMessage response)
        {
            HttpResponseResult result;

            try
            {
                response.EnsureSuccessStatusCode();
                result = Task.Run(async() => await response.Content.ReadAsStringAsync()).ContinueWith(m => new HttpResponseResult(m.Result, HttpStatusCode.OK)).Result;
            }
            catch (HttpRequestException ex)
            {
                result = new HttpResponseResult(ex.Message, HttpStatusCode.InternalServerError);
            }
            return(result);
        }
Пример #2
0
        public async Task <HttpResponseResult> Get(string url)
        {
            HttpResponseResult result = null;

            try
            {
                using (var http = this.InitHttp())
                {
                    var response = await http.GetAsync(url);

                    response.EnsureSuccessStatusCode();//保证请求成功,失败的话报错
                    result = await response.Content.ReadAsStringAsync().ContinueWith(m => new HttpResponseResult(m.Result, HttpStatusCode.OK));
                }
            }
            catch (HttpRequestException ex)
            {
                result = new HttpResponseResult(ex.Message, HttpStatusCode.InternalServerError);
            }
            return(result);
        }
Пример #3
0
        public async Task <HttpResponseResult> Post <T>(string url, T data)
        {
            HttpResponseResult result = null;

            if (data == null)
            {
                result = null;
            }

            using (var http = this.InitHttp())
            {
                string      json     = data.ToJson <T>();
                HttpContent content  = new StringContent(json, Encoding.UTF8, "application/json");
                var         response = await http.PostAsync(url, content);

                result = response.ConvertToResult();
                //response.EnsureSuccessStatusCode();//保证请求成功,失败的话报错
                //result = await response.Content.ReadAsStringAsync().ContinueWith(m => new HttpResponseResult(m.Result, HttpStatusCode.OK));
            }

            return(result);
        }