Вот пример HttpClient
на основе фрагмента из ответа OP
using System.Threading.Tasks;
using System.Net.Http;
using System.Text;
using System.Net.Http.Headers;
public class RESTClient
{
private readonly HttpClient client = new HttpClient();
public RESTClient(string baseAddress)
{
client.BaseAddress = new Uri(baseAddress);
}
public void SetAuthHeader(string parameter)
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", parameter);
}
public async Task<string> MakeRequestAsync(HttpMethod method, string path, string postContent = "")
{
try
{
using (HttpContent content = new StringContent(postContent, Encoding.UTF8, "application/json"))
using (HttpRequestMessage request = new HttpRequestMessage(method, path))
{
if (method == HttpMethod.Post || method == HttpMethod.Put) request.Content = content;
using (HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
{
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}
}
catch (Exception ex)
{
return "{\"errorMessages\":[\"" + ex.Message + "\"],\"errors\":{}}";
}
}
}
Пример использования (WinForms)
private RESTClient restClient;
private void Form1_Load(object sender, EventArgs e)
{
restClient = new RESTClient("https://myapi.url/api");
restClient.SetAuthHeader("iODA0NjMxMTgwMWUzYWFkYTk4NjM2MjcyOTk3MDowYTU0N2I2NzliNWRkMjliN2I4NTFlMDBkY2Y2NjQzNzQ5OTIxYzZl");
}
private async void button1_Click(object sender, EventArgs e)
{
// GET
string getJsonResult = await restClient.MakeRequestAsync(HttpMethod.Get, "path/to/method");
// POST
string postJsonResult = await restClient.MakeRequestAsync(HttpMethod.Post, "path/to/method", "{\"data\": \"Some Request Data\"}");
// process data here
}