public static T Invoke(string url, object input, bool requireJSON = true) { using (var client = new HttpClient()) { HttpContent httpContent = null; if (input != null) { string paramJson = string.Empty; if (requireJSON) { paramJson = JsonConvert.SerializeObject(input); } else { paramJson = Convert.ToString(input); } httpContent = new StringContent(paramJson); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); } var response = client.PostAsync(url, httpContent).Result; var result = response.Content.ReadAsStringAsync().Result; var output = JsonConvert.DeserializeObject (result); return output; } }
public static R Post3w (string url, P input) { using (HttpClient client = new HttpClient()) { HttpRequestMessage request = new HttpRequestMessage(); request.Method = HttpMethod.Post; request.RequestUri = new Uri(url); string inputJson = JsonConvert.SerializeObject(input); Dictionaryvalues = JsonConvert.DeserializeObject >(inputJson); //convert to key/value pairs request.Content = new FormUrlEncodedContent(values); var response = client.SendAsync(request).Result; var json = response.Content.ReadAsStringAsync().Result; return JsonConvert.DeserializeObject (json); } }