public class HttpClientHelper
{
public static string GetResponseJson(string url)
{
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = httpClient.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
string responseJson = response.Content.ReadAsStringAsync().Result;
return responseJson;
}
else
{
return "Error,StatusCode:" + response.StatusCode.ToString();
}
}
public static string PostResponseJson(string url, string requestJson)
{
HttpContent httpContent = new StringContent(requestJson);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
if (response.IsSuccessStatusCode)
{
string responseJson = response.Content.ReadAsStringAsync().Result;
return responseJson;
}
else
{
return "Error,StatusCode:" + response.StatusCode.ToString();
}
}
}