2024-03-12 16:15:15 +08:00
|
|
|
|
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;
|
|
|
|
|
};
|
2024-03-14 00:02:17 +08:00
|
|
|
|
HttpClient client = Initialize.hc_pool.GetHttpClient(request_type);
|
2024-03-12 16:15:15 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|