/// <summary> /// Serializes an object in either xml or json format /// </summary> private static StringBuilder SerializeObject(object obj) { var sb = new StringBuilder(); JsonIO.Serialize(obj, sb); return(sb); }
/// <summary> /// Calls the Payoff service method /// </summary> public static async Task <string> Payoff(ResolverData data) { var handler = new HttpClientHandler(); handler.PreAuthenticate = handler.SupportsPreAuthenticate(); using (var client = new HttpClient(handler)) { try { // formulate full url string url = string.Format(_payoffPathV2, data.Host, data.Payload, data.Type); AppendClientData(ref url, data); var now = DateTime.UtcNow; client.DefaultRequestHeaders.Date = now; string body = SerializeObject(CreateMetricV2()).ToString(); int lenBody = Encoding.UTF8.GetByteCount(body); var content = new StringContent(body); content.Headers.ContentType = MediaTypeHeaderValue.Parse(_contentType); // set user credentials in Authorization header string authInfo = CreateAuthorizationHeader(url, "POST", lenBody, _contentType, now); client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authInfo); // make request HttpResponseMessage response; try { Elapsed.Restart(); response = await client.PostAsync(url, content); } finally { Elapsed.Stop(); } StatusCode = response.StatusCode; if (!response.IsSuccessStatusCode) { return("StatusCode: " + StatusCode); } var result = response.Content.ReadAsStringAsync().Result; return(JsonIO.FormatFragment(result)); } catch (Exception e) { var error = string.Format("Exception: " + e.Message); if (e.InnerException != null) { error += string.Format("\nInner Exception: " + e.InnerException.Message); } return(error); } } }
/// <summary> /// Calls the IsAvailable service method /// </summary> public static async Task <string> IsAvailable(ResolverData data) { using (var client = new HttpClient()) { try { // formulate full url string url = string.Format(_availPathTestDb, data.Host); AppendClientData(ref url, data); // make request HttpResponseMessage response; try { Elapsed.Restart(); response = await client.GetAsync(url); } finally { Elapsed.Stop(); } StatusCode = response.StatusCode; if (!response.IsSuccessStatusCode) { return("StatusCode: " + StatusCode); } var result = response.Content.ReadAsStringAsync().Result; return(JsonIO.FormatFragment(result)); } catch (Exception e) { var error = string.Format("Exception: " + e.Message); if (e.InnerException != null) { error += string.Format("\nInner Exception: " + e.InnerException.Message); } return(error); } finally { Elapsed.Stop(); } } }