예제 #1
0
 public static string FormatString(string content)
 {
     if (IsXml(content))
     {
         return(TryFormatXml(content));
     }
     else if (IsJson(content))
     {
         return(General.TryFormatJson(content));
     }
     else
     {
         return(content);
     }
 }
        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));
        }