2024-03-12 16:15:15 +08:00
|
|
|
|
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<int> success;
|
|
|
|
|
Action<int, Exception, Get_Index_Result?> failed;
|
|
|
|
|
List<KeyValuePair<string, string>> respond_cookie = new List<KeyValuePair<string, string>>();
|
|
|
|
|
public Request_Type request_type { get; } = Request_Type.get_index;
|
|
|
|
|
public Get_Index(int _id, Action<int> _success, Action<int, Exception, Get_Index_Result?> _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
|
|
|
|
|
{
|
2024-03-14 00:02:17 +08:00
|
|
|
|
HttpClient client = Initialize.hc_pool.GetHttpClient(request_type);
|
2024-03-12 16:15:15 +08:00
|
|
|
|
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<string, string>(Key_ValuePair[0], Key_ValuePair[1]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
failed(id, ex, null);
|
|
|
|
|
}
|
|
|
|
|
success(id);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
public List<KeyValuePair<string, string>>? 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|