using Microsoft.Extensions.DependencyInjection;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;
namespace Bootstrap.Admin
{
///
/// HttpClient 扩展操作类
///
public static class HttpClientExtensions
{
/////
///// GetJson 异步方法
/////
/////
/////
/////
/////
//public static async Task GetAsJsonAsync(this HttpClient client, string requestUri = null)
//{
// var resp = await client.GetAsync(requestUri);
// var json = await resp.Content.ReadAsStringAsync();
// return JsonSerializer.Deserialize(json, new JsonSerializerOptions().AddDefaultConverters());
//}
/////
///// PostJson 异步方法
/////
/////
/////
/////
/////
/////
/////
//public static async Task PostAsJsonAsync(this HttpClient client, string requestUri, TValue t)
//{
// var resp = await client.PostAsJsonAsync(requestUri, t);
// var json = await resp.Content.ReadAsStringAsync();
// return JsonSerializer.Deserialize(json, new JsonSerializerOptions().AddDefaultConverters());
//}
/////
///// PostJson 异步方法
/////
/////
/////
/////
/////
/////
//public static async Task PostAsJsonAsync(this HttpClient client, TValue t) => await PostAsJsonAsync(client, string.Empty, t);
/////
///// DeleteJson 异步方法
/////
/////
/////
/////
/////
/////
/////
//public static async Task DeleteAsJsonAsync(this HttpClient client, string requestUri, TValue t)
//{
// var req = new HttpRequestMessage(HttpMethod.Delete, requestUri);
// req.Content = new StringContent(JsonSerializer.Serialize(t));
// req.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
// var resp = await client.SendAsync(req);
// var json = await resp.Content.ReadAsStringAsync();
// return JsonSerializer.Deserialize(json, new JsonSerializerOptions().AddDefaultConverters());
//}
/////
///// DeleteJson 异步方法
/////
/////
/////
/////
/////
/////
//public static async Task DeleteAsJsonAsync(this HttpClient client, TValue t) => await DeleteAsJsonAsync(client, string.Empty, t);
/////
///// PutJson 异步方法
/////
/////
/////
/////
/////
/////
/////
//public static async Task PutAsJsonAsync(this HttpClient client, string requestUri, TValue t)
//{
// var resp = await client.PutAsJsonAsync(requestUri, t);
// var json = await resp.Content.ReadAsStringAsync();
// return JsonSerializer.Deserialize(json, new JsonSerializerOptions().AddDefaultConverters());
//}
/////
///// PutJson 异步方法
/////
/////
/////
/////
/////
/////
//public static async Task PutAsJsonAsync(this HttpClient client, TValue t) => await PutAsJsonAsync(client, string.Empty, t);
///
/// LoginAsync 异步方法
///
///
///
///
///
public static async Task LoginAsync(this HttpClient client, string userName = "Admin", string password = "123789")
{
var r = await client.GetAsync("/Account/Login");
var view = await r.Content.ReadAsStringAsync();
var tokenTag = "");
var antiToken = view.Substring(0, index);
var content = new MultipartFormDataContent
{
{ new StringContent(userName), "userName" },
{ new StringContent(password), "password" },
{ new StringContent("true"), "remember" },
{ new StringContent(antiToken), "__RequestVerificationToken" }
};
await client.PostAsync("/Account/Login", content);
}
}
}