Как отправить POST-запрос?
curl -X POST "https://shikimori.one/oauth/token" \
-H "User-Agent: appp" \
-F grant_type="refresh_token" \
-F client_id="id" \
-F client_secret="secret" \
-F refresh_token="token"
Ответы (1 шт):
Автор решения: aepot
→ Ссылка
class Program
{
private static readonly HttpClient _client = new HttpClient();
static async Task Main(string[] args)
{
_client.DefaultRequestHeaders.UserAgent.ParseAdd("appp");
Dictionary<string, string> data = new Dictionary<string, string>
{
["grant_type"] = "refresh_token",
["client_id"] = "id",
["client_secret"] = "secret",
["refresh_token"] = "token"
};
try
{
string response = await PostFormAsync("https://shikimori.one/oauth/token", data);
Console.WriteLine(response);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("Done.");
Console.ReadKey();
}
public static async Task<string> PostFormAsync(string url, Dictionary<string, string> formData)
{
using var content = new FormUrlEncodedContent(formData);
using var request = new HttpRequestMessage(HttpMethod.Post, url) { Content = content };
using var response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
return await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync();
}
}