using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; using HtmlAgilityPack; using Microsoft.Extensions.Configuration; using HtmlDocument = HtmlAgilityPack.HtmlDocument; namespace CSS_Solution.Request { internal class Get_Index : IRequest { Task task; readonly int id; string rnd = ""; string? url; Action success; Action failed; List> respond_cookie = new List>(); public Request_Type request_type { get; } = Request_Type.get_index; public Get_Index(int _id, Action _success, Action _failed) { id = _id; success = _success; failed = _failed; url = Initialize.configuration?.GetSection("login_index").Value; if (url == null || url.Length == 0) throw new ConfigurationErrorsException("Can't get login_index url from AppSettings.json"); task = new Task(async () => { try { HttpClient client = Initialize.hc_pool.GetHttpClient(request_type); HttpRequestMessage request_Message = new HttpRequestMessage(HttpMethod.Get, url); List<(string, string)> request_header = Settings.get_Index_Header.Get_header(); foreach (var (key, value) in request_header) request_Message.Headers.Add(key, value); HttpResponseMessage message = await client.SendAsync(request_Message); message.EnsureSuccessStatusCode(); HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(await message.Content.ReadAsStringAsync()); var node = htmlDoc.DocumentNode.SelectSingleNode("//input[@id='rnd']"); rnd = node.GetAttributeValue("value", ""); foreach (var header in message.Headers) { if (header.Key != "Set-Cookie") continue; foreach (var item in header.Value) foreach (var item1 in item.Split("; ")) { string[] Key_ValuePair = item1.Split('='); respond_cookie.Add(new KeyValuePair(Key_ValuePair[0], Key_ValuePair[1])); } } } catch (Exception ex) { failed(id, ex, null); } success(id); }); } public List>? Get_cookie() { if (!task.IsCompleted) return null; return respond_cookie; } public string? Get_rnd() => rnd; public int Get_Id() => id; public Task Run() { task.Start(); return task; } } }