public static string Request()
{
var result = Task.Run(() => GetResponse()).Result;
return result;
}
public static async Task GetResponse()
{
var httpClient = new HttpClient();
string responseBody = string.Empty;
try
{
string resourceAddress = "......";
string messageJson = "[.......]";
httpClient.DefaultRequestHeaders.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await httpClient
.PostAsync(resourceAddress, new StringContent(messageJson, Encoding.UTF8, "application/json"));
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
}
catch (HttpRequestException hrex)
{
//notification
}
catch (TaskCanceledException tcex)
{
//notification
}
catch (Exception ex)
{
//notification
}
finally
{
if (httpClient != null)
{
httpClient.Dispose();
httpClient = null;
}
}
return responseBody;
}