Archived
添加项目文件。
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CSS_Solution.Request
|
||||
{
|
||||
internal class Course_Request
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
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
|
||||
{
|
||||
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<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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CSS_Solution.Request
|
||||
{
|
||||
public class HttpClientPool
|
||||
{
|
||||
HttpClient[] httpClients = new HttpClient[5];
|
||||
public HttpClientPool()
|
||||
{
|
||||
for (int i = 0; i < httpClients.Length; i++)
|
||||
if(i==(int)Request_Type.to_course)
|
||||
httpClients[i] = new HttpClient(new HttpClientHandler() { AllowAutoRedirect = false });
|
||||
else
|
||||
httpClients[i] = new HttpClient();
|
||||
}
|
||||
public HttpClient getHttpClient(Request_Type request_Type) => httpClients[(int)request_Type];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CSS_Solution.Request
|
||||
{
|
||||
internal interface IRequest
|
||||
{
|
||||
List<KeyValuePair<string, string>>? Get_cookie();
|
||||
public Request_Type request_type { get; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Security.Policy;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
namespace CSS_Solution.Request
|
||||
{
|
||||
internal class Login : IRequest
|
||||
{
|
||||
Action<int> success;
|
||||
Action<int, Exception, Login_Result?> failed;
|
||||
string? getCode_url;
|
||||
string? login_url;
|
||||
string? code;
|
||||
string? JSESSIONID;
|
||||
Task task;
|
||||
List<KeyValuePair<string, string>> respond_cookie = new List<KeyValuePair<string, string>>();
|
||||
readonly int id;
|
||||
public Request_Type request_type => Request_Type.login;
|
||||
public Login(int _id, Get_Index_Result last_result, Action<int> _success, Action<int, Exception, Login_Result?> _failed)
|
||||
{
|
||||
id = _id;
|
||||
success = _success;
|
||||
failed = _failed;
|
||||
getCode_url = Initialize.configuration?.GetSection("getcode").Value;
|
||||
if (getCode_url == null || getCode_url.Length == 0)
|
||||
throw new ConfigurationErrorsException("Can't get getcode url from AppSettings.json");
|
||||
login_url = Initialize.configuration?.GetSection("login_index").Value;
|
||||
if (login_url == null || login_url.Length == 0)
|
||||
throw new ConfigurationErrorsException("Can't get login_index url from AppSettings.json");
|
||||
if (last_result.RND == null || last_result.JSESSIONID == null)
|
||||
throw new InvalidOperationException("Something wrong with Get_Index result");
|
||||
JSESSIONID = last_result.JSESSIONID;
|
||||
task = new Task(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
{
|
||||
HttpClient client = Initialize.hc_pool.getHttpClient(Request_Type.get_code);
|
||||
HttpRequestMessage code_request_Message = new HttpRequestMessage(HttpMethod.Post, getCode_url);
|
||||
code_request_Message.Headers.Add("Cookie", JSESSIONID);
|
||||
foreach (var (key, value) in Settings.getCode_Header.Get_Request_header())
|
||||
code_request_Message.Headers.Add(key, value);
|
||||
code_request_Message.Content = new FormUrlEncodedContent([new KeyValuePair<string, string>("webfinger", Settings.webfinger.Get_webfinger().Item2)]);
|
||||
code_request_Message.Content.Headers.ContentType = new MediaTypeHeaderValue(Settings.getCode_Header.Content_Type);
|
||||
code_request_Message.Content.Headers.ContentLength = int.Parse(Settings.getCode_Header.Content_Length);
|
||||
HttpResponseMessage code_message = await client.SendAsync(code_request_Message);
|
||||
code_message.EnsureSuccessStatusCode();
|
||||
code = await code_message.Content.ReadAsStringAsync();
|
||||
}
|
||||
{
|
||||
HttpClient client = Initialize.hc_pool.getHttpClient(request_type);
|
||||
HttpRequestMessage login_request_Message = new HttpRequestMessage(HttpMethod.Post, login_url);
|
||||
login_request_Message.Headers.Add("Cookie", JSESSIONID);
|
||||
foreach (var (key, value) in Settings.login_Header.Get_Request_header())
|
||||
login_request_Message.Headers.Add(key, value);
|
||||
List<KeyValuePair<string, string>> response_body = Settings.login_Body.Get_Request_body(last_result.RND, code);
|
||||
login_request_Message.Content = new FormUrlEncodedContent(response_body);
|
||||
login_request_Message.Content.Headers.ContentType = new MediaTypeHeaderValue(Settings.login_Header.Content_Type);
|
||||
HttpResponseMessage message = await client.SendAsync(login_request_Message);
|
||||
message.EnsureSuccessStatusCode();
|
||||
string ans = await message.Content.ReadAsStringAsync();
|
||||
|
||||
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('=');
|
||||
if (Key_ValuePair.Length != 2)
|
||||
continue;
|
||||
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()
|
||||
{
|
||||
return respond_cookie;
|
||||
}
|
||||
public Task Run()
|
||||
{
|
||||
task.Start();
|
||||
return task;
|
||||
}
|
||||
|
||||
internal int Get_Id() => id;
|
||||
public string Get_JSESSIONID() => JSESSIONID ?? "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CSS_Solution.Request
|
||||
{
|
||||
class Get_Index_Result
|
||||
{
|
||||
public int? id;
|
||||
public string? JSESSIONID;
|
||||
public string? RND;
|
||||
public DateTime dateTime;
|
||||
private string? error_info;
|
||||
public string Error_info
|
||||
{
|
||||
get { return error_info ??= ""; }
|
||||
set
|
||||
{
|
||||
error_info = value;
|
||||
is_error = true;
|
||||
}
|
||||
}
|
||||
bool is_error;
|
||||
public Get_Index_Result(int _id, string jSESSIONID, string rND, DateTime dateTime)
|
||||
{
|
||||
id = _id;
|
||||
JSESSIONID = jSESSIONID;
|
||||
RND = rND;
|
||||
error_info = "";
|
||||
this.dateTime = dateTime;
|
||||
}
|
||||
public Get_Index_Result(Get_Index get_Index)
|
||||
{
|
||||
id = get_Index.Get_Id();
|
||||
JSESSIONID = get_Index.Get_cookie()?.Select(element => new { element.Key, element.Value }).Where(element => element.Key.Equals("JSESSIONID")).First().Value;
|
||||
RND = get_Index.Get_rnd();
|
||||
dateTime = DateTime.Now;
|
||||
error_info = "";
|
||||
}
|
||||
public Get_Index_Result(string error_info) => Error_info = error_info;
|
||||
public bool Wrong_Result() => JSESSIONID == null || RND == null;
|
||||
public bool isError() => is_error;
|
||||
}
|
||||
class Login_Result
|
||||
{
|
||||
public List<KeyValuePair<string, string>> cookie = new List<KeyValuePair<string, string>>();
|
||||
public int? id;
|
||||
public string? JSESSIONID;
|
||||
public string? CERLOGIN;
|
||||
public DateTime dateTime;
|
||||
private string? error_info;
|
||||
public string Error_info
|
||||
{
|
||||
get { return error_info ??= ""; }
|
||||
set
|
||||
{
|
||||
error_info = value;
|
||||
is_error = true;
|
||||
}
|
||||
}
|
||||
bool is_error;
|
||||
|
||||
public Login_Result(int _id, string jSESSIONID, string cERLOGIN, DateTime dateTime)
|
||||
{
|
||||
id = _id;
|
||||
JSESSIONID = jSESSIONID;
|
||||
CERLOGIN = cERLOGIN;
|
||||
dateTime = DateTime.Now;
|
||||
error_info = "";
|
||||
cookie = new List<KeyValuePair<string, string>>();
|
||||
cookie.Add(new KeyValuePair<string, string>("JSESSIONID", JSESSIONID));
|
||||
cookie.Add(new KeyValuePair<string, string>("CERLOGIN", CERLOGIN));
|
||||
}
|
||||
public Login_Result(Login login)
|
||||
{
|
||||
id = login.Get_Id();
|
||||
cookie = login.Get_cookie() ?? throw new InvalidOperationException("Cookie of Login is NULL?!");
|
||||
for (int i = 0; i < cookie.Count; i++)
|
||||
if (cookie[i].Key == "JSESSIONID" || cookie[i].Key == "Path")
|
||||
{
|
||||
cookie.RemoveAt(i);
|
||||
i--;
|
||||
}
|
||||
JSESSIONID = login.Get_JSESSIONID();
|
||||
CERLOGIN = cookie?.Select(x => x).Where(element => element.Key.Equals("CERLOGIN")).First().Value;
|
||||
cookie?.Add(new KeyValuePair<string, string>("JSESSIONID", JSESSIONID));
|
||||
dateTime = DateTime.Now;
|
||||
error_info = "";
|
||||
}
|
||||
public Login_Result(string error_info) => Error_info = error_info;
|
||||
public bool Wrong_Result() => JSESSIONID == null || CERLOGIN == null || cookie.Select(x => new { havenull = x.Value == null }).Where(x => x.havenull).Count() > 0;
|
||||
public bool isError() => is_error;
|
||||
public List<KeyValuePair<string, string>> Get_cookie() => cookie;
|
||||
}
|
||||
class To_Course_Result
|
||||
{
|
||||
public List<KeyValuePair<string, string>> cookie = new List<KeyValuePair<string, string>>();
|
||||
public int? id;
|
||||
public string? JSESSIONID;
|
||||
public string? BINGOCLOUDLB;
|
||||
public DateTime dateTime;
|
||||
private string? error_info;
|
||||
public string Error_info
|
||||
{
|
||||
get { return error_info ??= ""; }
|
||||
set
|
||||
{
|
||||
error_info = value;
|
||||
is_error = true;
|
||||
}
|
||||
}
|
||||
bool is_error;
|
||||
public To_Course_Result(int _id,string jSESSIONID,string bINGOCLOUDLB,DateTime dateTime)
|
||||
{
|
||||
id = _id;
|
||||
JSESSIONID = jSESSIONID;
|
||||
BINGOCLOUDLB = bINGOCLOUDLB;
|
||||
error_info = "";
|
||||
this.dateTime = dateTime;
|
||||
}
|
||||
public To_Course_Result(To_Course to_Course)
|
||||
{
|
||||
id = to_Course.get_ID();
|
||||
cookie = to_Course.Get_cookie() ?? throw new InvalidOperationException("Cookie of ToCourse is NULL?!");
|
||||
JSESSIONID = cookie?.Select(x => x).Where(element => element.Key.Equals("JSESSIONID")).First().Value;
|
||||
BINGOCLOUDLB = cookie?.Select(x => x).Where(element => element.Key.Equals("BINGOCLOUDLB")).First().Value;
|
||||
dateTime = DateTime.Now;
|
||||
error_info = "";
|
||||
}
|
||||
public To_Course_Result(string error_info) => Error_info = error_info;
|
||||
public bool Wrong_Result() => JSESSIONID == null || BINGOCLOUDLB == null || cookie.Select(x => new { havenull = x.Value == null }).Where(x => x.havenull).Count() > 0;
|
||||
public bool isError() => is_error;
|
||||
public List<KeyValuePair<string, string>> Get_cookie() => cookie;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CSS_Solution.Request
|
||||
{
|
||||
class Task_handler
|
||||
{
|
||||
public static Index_Task_handler index_Task_Handler = new Index_Task_handler();
|
||||
public static Login_Task_handler login_Task_Handler = new Login_Task_handler();
|
||||
public static ToCourse_Task_Handler toCourse_Task_Handler = new ToCourse_Task_Handler();
|
||||
}
|
||||
class Index_Task_handler
|
||||
{
|
||||
int current_id = 0;
|
||||
public LinkedList<Get_Index_Result> results = new LinkedList<Get_Index_Result>();
|
||||
Dictionary<int, (Get_Index, Action<Get_Index_Result>)> tasks = new Dictionary<int, (Get_Index, Action<Get_Index_Result>)>();
|
||||
public void Make_request(Action<Get_Index_Result> result_handler)
|
||||
{
|
||||
current_id++;
|
||||
Get_Index get_Index = new Get_Index(current_id, GetIndex_success, GetIndex_failed);
|
||||
get_Index.Run();
|
||||
tasks.Add(current_id, (get_Index, result_handler));
|
||||
}
|
||||
public void GetIndex_success(int id)
|
||||
{
|
||||
Action<Get_Index_Result> result_handler;
|
||||
Get_Index_Result result;
|
||||
if (tasks.TryGetValue(id, out (Get_Index, Action<Get_Index_Result>) value))
|
||||
{
|
||||
Get_Index task = value.Item1;
|
||||
result_handler = value.Item2;
|
||||
result = new Get_Index_Result(task);
|
||||
if (result.Wrong_Result())
|
||||
{
|
||||
GetIndex_failed(id, new HttpRequestException("Get_Index Result info error, Check it out!"), result);
|
||||
return;
|
||||
}
|
||||
results.AddFirst(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
GetIndex_failed(id, new KeyNotFoundException($"Get_Index: who called me? I even don't have ID {id}"));
|
||||
return;
|
||||
}
|
||||
tasks.Remove(id);
|
||||
result_handler(result);
|
||||
}
|
||||
public void GetIndex_failed(int id, Exception e, Get_Index_Result? result = null)
|
||||
{
|
||||
if (e.GetType() == typeof(KeyNotFoundException))
|
||||
{
|
||||
Initialize.default_logger.LogWarning($"[{DateTime.Now}] ToIndex: {e.Message}");
|
||||
return;
|
||||
}
|
||||
if (result == null)
|
||||
result = new Get_Index_Result(e.Message);
|
||||
else
|
||||
result.Error_info = e.Message;
|
||||
tasks[id].Item2(result);
|
||||
tasks.Remove(id);
|
||||
}
|
||||
public Get_Index_Result Newest_Result() => results.First();
|
||||
}
|
||||
class Login_Task_handler
|
||||
{
|
||||
int current_id = 0;
|
||||
public LinkedList<Login_Result> results = new LinkedList<Login_Result>();
|
||||
Dictionary<int, (Login, Action<Login_Result>)> tasks = new Dictionary<int, (Login, Action<Login_Result>)>();
|
||||
public void Make_request(Action<Login_Result> result_handler, Get_Index_Result? last_result)
|
||||
{
|
||||
current_id++;
|
||||
last_result ??= Task_handler.index_Task_Handler.Newest_Result();
|
||||
if (last_result == null)
|
||||
throw new InvalidOperationException("No usable Get_Index_Result for Login");
|
||||
Login login = new Login(current_id, last_result, Login_success, Login_failed);
|
||||
login.Run();
|
||||
tasks.Add(current_id, (login, result_handler));
|
||||
}
|
||||
public void Login_success(int id)
|
||||
{
|
||||
Action<Login_Result> result_handler;
|
||||
Login_Result result;
|
||||
if (tasks.TryGetValue(id, out (Login, Action<Login_Result>) value))
|
||||
{
|
||||
Login task = value.Item1;
|
||||
result_handler = value.Item2;
|
||||
result = new Login_Result(task);
|
||||
if (result.Wrong_Result())
|
||||
{
|
||||
Login_failed(id, new HttpRequestException("Login Result info error, Check it out!"), result);
|
||||
return;
|
||||
}
|
||||
results.AddFirst(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
Login_failed(id, new KeyNotFoundException($"Login: who called me? I even don't have ID {id}"));
|
||||
return;
|
||||
}
|
||||
tasks.Remove(id);
|
||||
result_handler(result);
|
||||
}
|
||||
public void Login_failed(int id, Exception e, Login_Result? result = null)
|
||||
{
|
||||
if (e.GetType() == typeof(KeyNotFoundException))
|
||||
{
|
||||
Initialize.default_logger.LogWarning($"[{DateTime.Now}] Login: {e.Message}");
|
||||
return;
|
||||
}
|
||||
if (result == null)
|
||||
result = new Login_Result(e.Message);
|
||||
else
|
||||
result.Error_info = e.Message;
|
||||
tasks[id].Item2(result);
|
||||
tasks.Remove(id);
|
||||
}
|
||||
public List<KeyValuePair<string,string>>? Newest_cookie() => results.First()?.cookie;
|
||||
public Login_Result Newest_result() => results.First();
|
||||
}
|
||||
class ToCourse_Task_Handler
|
||||
{
|
||||
int current_id = 0;
|
||||
public LinkedList<To_Course_Result>results = new LinkedList<To_Course_Result>();
|
||||
Dictionary<int, (To_Course, Action<To_Course_Result>)> tasks = new Dictionary<int, (To_Course, Action<To_Course_Result>)>();
|
||||
public void Make_request(Action<To_Course_Result> result_handler,Login_Result? last_result)
|
||||
{
|
||||
current_id++;
|
||||
last_result ??=Task_handler.login_Task_Handler.Newest_result();
|
||||
if(last_result == null)
|
||||
throw new InvalidOperationException("No usable Login_Result for Login");
|
||||
To_Course to_Course = new To_Course(current_id, last_result,To_Course_success,To_Course_failed);
|
||||
to_Course.Run();
|
||||
tasks.Add(current_id, (to_Course, result_handler));
|
||||
}
|
||||
public void To_Course_success(int id)
|
||||
{
|
||||
Action<To_Course_Result> result_handler;
|
||||
To_Course_Result result;
|
||||
if(tasks.TryGetValue(id,out (To_Course,Action<To_Course_Result>) value))
|
||||
{
|
||||
To_Course task = value.Item1;
|
||||
result_handler = value.Item2;
|
||||
result = new To_Course_Result(task);
|
||||
if(result.Wrong_Result())
|
||||
{
|
||||
To_Course_failed(id, new HttpRequestException("To_Course Result info error, Check it out!"), result);
|
||||
return;
|
||||
}
|
||||
results.AddLast(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
To_Course_failed(id, new KeyNotFoundException($"To_Course: who called me? I even don't have ID {id}"));
|
||||
return;
|
||||
}
|
||||
tasks.Remove(id);
|
||||
result_handler(result);
|
||||
}
|
||||
|
||||
public void To_Course_failed(int id,Exception e,To_Course_Result? result = null)
|
||||
{
|
||||
if (e.GetType() == typeof(KeyNotFoundException))
|
||||
{
|
||||
Initialize.default_logger.LogWarning($"[{DateTime.Now}] Login: {e.Message}");
|
||||
return;
|
||||
}
|
||||
if(result==null)
|
||||
result = new To_Course_Result(e.Message);
|
||||
else
|
||||
result.Error_info = e.Message;
|
||||
tasks[id].Item2(result);
|
||||
tasks.Remove(id);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CSS_Solution.Request
|
||||
{
|
||||
internal class To_Course : IRequest
|
||||
{
|
||||
Task task;
|
||||
readonly int id;
|
||||
string? url;
|
||||
string? Referer;
|
||||
Action<int> success;
|
||||
Action<int, Exception, To_Course_Result?> failed;
|
||||
List<KeyValuePair<string, string>> respond_cookie = new List<KeyValuePair<string, string>>();
|
||||
string? Course_cookie, Certification_cookie;
|
||||
public Request_Type request_type => Request_Type.to_course;
|
||||
public To_Course(int _id, Login_Result last_result, Action<int> _success, Action<int, Exception, To_Course_Result?> _failed)
|
||||
{
|
||||
id = _id;
|
||||
success = _success;
|
||||
failed = _failed;
|
||||
url = Initialize.configuration?.GetSection("to_course").Value;
|
||||
if (url == null || url.Length == 0)
|
||||
throw new ConfigurationErrorsException("Can't get to_course url from AppSettings.json");
|
||||
task = new Task(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Func<string?, HttpRequestMessage> Get_HttpRequestMessage = (string? url) =>
|
||||
{
|
||||
if (url == null || url.Length == 0)
|
||||
throw new ConfigurationErrorsException("The url for Request is null");
|
||||
HttpRequestMessage request_Message = new HttpRequestMessage(HttpMethod.Post, url);
|
||||
request_Message.Headers.Add("Cookie", "CERLOGIN" + "=" + last_result.CERLOGIN + "; BINGOCLOUDLB=f2adbbd5386222824901cc921da810f4");
|
||||
foreach (var (key, value) in Settings.to_Course_Header.Get_header())
|
||||
request_Message.Headers.Add(key, value);
|
||||
return request_Message;
|
||||
};
|
||||
HttpClient client = Initialize.hc_pool.getHttpClient(request_type);
|
||||
HttpResponseMessage message;
|
||||
do
|
||||
{
|
||||
message = await client.SendAsync(Get_HttpRequestMessage(url));
|
||||
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('=');
|
||||
if (Key_ValuePair.Length != 2)
|
||||
continue;
|
||||
respond_cookie.Add(new KeyValuePair<string, string>(Key_ValuePair[0], Key_ValuePair[1]));
|
||||
}
|
||||
}
|
||||
url = message?.Headers?.Location?.ToString() ?? url;
|
||||
if (message == null)
|
||||
throw new Exception("Internet Error, response is null.");
|
||||
} while (message.StatusCode == HttpStatusCode.Redirect);
|
||||
message.EnsureSuccessStatusCode();
|
||||
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('=');
|
||||
if (Key_ValuePair.Length != 2)
|
||||
continue;
|
||||
respond_cookie.Add(new KeyValuePair<string, string>(Key_ValuePair[0], Key_ValuePair[1]));
|
||||
}
|
||||
}
|
||||
//Need Del
|
||||
string ans = await message.Content.ReadAsStringAsync();
|
||||
string s = await message.Content.ReadAsStringAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
failed(id, ex, null);
|
||||
}
|
||||
Cookie_parse();
|
||||
Referer = url;
|
||||
|
||||
success(id);
|
||||
});
|
||||
}
|
||||
private void Cookie_parse()
|
||||
{
|
||||
for (int i = 0; i < respond_cookie.Count ; i++)
|
||||
{
|
||||
if (respond_cookie[i].Key == "Path")
|
||||
{
|
||||
if (respond_cookie[i].Value == "/Course")
|
||||
Course_cookie = respond_cookie[i - 1].Value;
|
||||
if (respond_cookie[i].Value == "/Certification")
|
||||
Certification_cookie = respond_cookie[i - 1].Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
public List<KeyValuePair<string, string>>? Get_cookie()
|
||||
{
|
||||
if (!task.IsCompleted)
|
||||
return null;
|
||||
return respond_cookie;
|
||||
}
|
||||
|
||||
public int get_ID() => id;
|
||||
public Task Run()
|
||||
{
|
||||
task.Start();
|
||||
return task;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user