示例#1
0
        public static HttpResponseMessage PostAsJsonAsync(
            this HttpClient client,
            string requestUri,
            JObject json,
            Action <string> Logger)
        {
            AddUserAgent(client);

            LogRequest(
                HttpMethod.Post.Method,
                client.BaseAddress + requestUri,
                client.DefaultRequestHeaders,
                JsonConvert.SerializeObject(json, Formatting.Indented),
                Logger);
            HttpResponseMessage response = client.PostAsJsonAsync(requestUri, json).Result;
            string content = response.EnsureSuccessStatusCode().Content.ReadAsStringAsync().Result;

            LogResponse(
                response.StatusCode.ToString(),
                response.Headers,
                General.TryFormatJson(content),
                Logger);

            return(response);
        }
示例#2
0
        public static T GetJson <T>(this HttpClient client, string requestUri, Action <string> Logger)
        {
            AddUserAgent(client);
            LogRequest(
                HttpMethod.Get.Method,
                client.BaseAddress + requestUri,
                client.DefaultRequestHeaders,
                string.Empty,
                Logger);
            HttpResponseMessage response = client.GetAsync(requestUri).Result;
            string content = response.EnsureSuccessStatusCode().Content.ReadAsStringAsync().Result;

            LogResponse(response.StatusCode.ToString(), response.Headers, General.TryFormatJson(content), Logger);

            return(JsonConvert.DeserializeObject <T>(content));
        }
示例#3
0
        private static T CallRestApiWithJsonPayload <T>(
            this HttpClient client,
            string requestUri,
            T json,
            Action <string> logger,
            string method)
        {
            AddUserAgent(client);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            LogRequest(
                method,
                client.BaseAddress + requestUri,
                client.DefaultRequestHeaders,
                JsonConvert.SerializeObject(json, Formatting.Indented),
                logger);
            HttpResponseMessage response;

            switch (method.ToUpper())
            {
            case WebRequestMethods.Http.Post: response = client.PostAsJsonAsync(requestUri, json).Result; break;

            case WebRequestMethods.Http.Put: response = client.PutAsJsonAsync(requestUri, json).Result; break;

            default: throw new InvalidOperationException();
            }

            string content = response.EnsureSuccessStatusCode().Content.ReadAsStringAsync().Result;

            LogResponse(
                response.StatusCode.ToString(),
                response.Headers,
                General.TryFormatJson(content),
                logger);

            return(JsonConvert.DeserializeObject <T>(content));
        }