public T Get <T>(string action, bool isExternal = false, string sectionKey = "") { ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; using (var client = new HttpClient()) { if (isExternal && sectionKey != "") { client.AddExternalHeaders(sectionKey); } else { client.AddHeaders(); } var result = client.GetAsync(BuildActionUri(action)).Result; string json = result.Content.ReadAsStringAsync().Result; if (result.IsSuccessStatusCode) { return(JsonConvert.DeserializeObject <T>(json)); } throw new ApiException(result.StatusCode, json); } }
public void given_null_headers_object_should_not_throw() { object headers = null; var client = new HttpClient(); Action action = () => client.AddHeaders(headers); action.ShouldNotThrow(); }
given_new_typed_headers_with_underscores_in_name_should_add_header_names_with_dashes() { dynamic headers = new { x_medassets_auth = "auth", x_requested_with = "XmlHttpRequest" }; var client = new HttpClient(); client.AddHeaders(headers as object); client.DefaultRequestHeaders.GetValues("x-medassets-auth").First().Should().Be(headers.x_medassets_auth); client.DefaultRequestHeaders.GetValues("x-requested-with").First().Should().Be(headers.x_requested_with); }
public void given_new_typed_headers_should_add_headers_by_its_string_value() { dynamic headers = new { Id = Guid.NewGuid(), Strings = new string[2] }; var client = new HttpClient(); client.AddHeaders(headers as object); client.DefaultRequestHeaders.GetValues("Id").First().Should().Be(headers.Id.ToString()); client.DefaultRequestHeaders.GetValues("Strings").First().Should().Be(headers.Strings.ToString()); }
public void given_new_string_headers_should_add_headers() { dynamic headers = new { FirstName = "Todd", LastName = "Meinershagen" }; var client = new HttpClient(); client.AddHeaders(headers as object); client.DefaultRequestHeaders.GetValues("FirstName").First().Should().Be(headers.FirstName); client.DefaultRequestHeaders.GetValues("LastName").First().Should().Be(headers.LastName); }
private static async Task <HttpResponseMessage> PerformGetAsync(Headers headers, InvokeHttpRequest req, HttpClient client) { client.AddHeaders(headers); var httpResponse = await client.GetAsync(req.Url); if (!httpResponse.IsSuccessStatusCode) { throw new ProxyHttpException(httpResponse.StatusCode, httpResponse.ReasonPhrase); } return(httpResponse); }
public void Get(string action) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; using (var client = new HttpClient()) { client.AddHeaders(); var result = client.GetAsync(BuildActionUri(action)).Result; var json = result.Content.ReadAsStringAsync().Result; throw new ApiException(result.StatusCode, json); } }
private static async Task <HttpResponseMessage> PerformPostAsync(ISerializer serializer, Headers headers, InvokeHttpRequest req, HttpClient client) { client.AddHeaders(headers); var content = CreateJsonContent(serializer, req); var respose = await client.PostAsync(req.Url, content); if (!respose.IsSuccessStatusCode) { throw new ProxyHttpException(respose.StatusCode, respose.ReasonPhrase); } return(respose); }
internal static Task PostAsync(ISerializer serializer, Headers headers, InvokeHttpRequest req) { return(Task.Run(async() => { using (HttpClient client = new HttpClient()) { client.AddHeaders(headers); StringContent requestContent = CreateJsonContent(serializer, req); await client.PostAsync(req.Url, requestContent); } })); }
/// <summary> /// Issues an HTTP Get to the specified URI with the specified headers /// </summary> /// <param name="uri">The address of the service to post to</param> /// <param name="headers">A collection of name/value pairs to be added to the request as headers</param> /// <returns>A Task containing an HttpResponseMessage that holds the result of the post operation</returns> public async Task <IHttpResponseMessage> GetAsync(string uri, IEnumerable <KeyValuePair <string, string> > headers) { var client = new HttpClient(); client.AddHeaders(headers); Console.WriteLine($"Issuing an HTTP Get to '{uri}'."); var response = await client.GetAsync(uri); var responseText = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Response returned from HTTP Get request: {responseText}"); return(response.AsIHttpResponseMessage()); }
public T Post <T>(string action, object data) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; using (var client = new HttpClient()) { client.AddHeaders(); var result = client.PostAsJsonAsync(BuildActionUri(action), data).Result; string json = result.Content.ReadAsStringAsync().Result; if (result.IsSuccessStatusCode) { return(JsonConvert.DeserializeObject <T>(json)); } throw new ApiException(result.StatusCode, json); } }
public async Task PostAsync(string action, object data = null) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; using (var client = new HttpClient()) { client.AddHeaders(); var result = await client.PostAsJsonAsync(BuildActionUri(action), data); if (result.IsSuccessStatusCode) { return; } string json = await result.Content.ReadAsStringAsync(); throw new ApiException(result.StatusCode, json); } }
public async Task <T> AuthenticateAsync <T>(string userName, string password) { using (var client = new HttpClient()) { client.AddHeaders(); var result = await client.PostAsync(BuildActionUri("token"), new FormUrlEncodedContent(new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("userName", userName), new KeyValuePair <string, string>("password", password) })); string json = await result.Content.ReadAsStringAsync(); if (result.IsSuccessStatusCode) { var res = json.Replace("\\\"", "\"").Replace("\"[", "[").Replace("]\"", "]").Replace("\"{", "{").Replace("}\"", "}"); return(JsonConvert.DeserializeObject <T>(res)); } throw new ApiException(result.StatusCode, json); } }