コード例 #1
0
ファイル: Client.cs プロジェクト: mattschwartz/mab
        public static async Task<ApiResponse> PostAsync(string api, object data)
        {
            try {
                using (var client = new HttpClient()) {
                    client.BaseAddress = new Uri(_baseUrl);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    var content = new StringContent(JsonConvert.SerializeObject(data));
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    HttpResponseMessage response = await client.PostAsync(api, content);

                    string responseContent = await response.Content.ReadAsStringAsync();

                    var apiResponse = new ApiResponse
                    {
                        Status = response.StatusCode,
                        Message = responseContent
                    };

                    return apiResponse;
                }
            } catch (Exception ex) {
                return new ApiResponse
                {
                    Status = HttpStatusCode.InternalServerError,
                    Message = string.Format("({0}) An unexpected exception has occurred: {1}, trace: {2}", HttpStatusCode.ServiceUnavailable, ex.Message, ex.StackTrace)
                };
            }
        }
コード例 #2
0
ファイル: Client.cs プロジェクト: mattschwartz/mab
        public static async Task<ApiResponse> GetAsync(string api)
        {
            try {
                using (var client = new HttpClient()) {
                    client.BaseAddress = new Uri(_baseUrl);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    HttpResponseMessage response = await client.GetAsync(api);

                    string responseJson = await response.Content.ReadAsStringAsync();

                    var apiResponse = new ApiResponse
                    {
                        Status = response.StatusCode,
                        Message = responseJson
                    };

                    return apiResponse;
                }
            } catch {
                return new ApiResponse
                {
                    Status = HttpStatusCode.InternalServerError,
                    Message = string.Format("({0}) An unexpected exception has occurred", HttpStatusCode.ServiceUnavailable)
                };
            }
        }