ZeroLauncher/Manager/LoginManager.cs
2024-05-12 16:43:07 +08:00

92 lines
3.8 KiB
C#

using Newtonsoft.Json.Linq;
using System.Net;
using System.Net.Http;
namespace Zerolauncher.Manager
{
internal class LoginManager
{
//private static readonly HttpClient client = new HttpClient();
public static async Task SendPostRequest()
{
var client = new HttpClient();
client.DefaultRequestVersion = HttpVersion.Version20;
client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
var values = new Dictionary<string, string>
{
{ "thing1", "hello" },
{ "thing2", "world" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
}
public static async Task LoginTest()
{
var client = new HttpClient();
client.DefaultRequestVersion = HttpVersion.Version20;
client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
var values = new Dictionary<string, string>
{
{ "thing1", "hello" },
{ "thing2", "world" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
}
public static async Task<string?> AssetNet(HttpClient client, SingleGame game, string url, FormUrlEncodedContent? content =null)
{
try
{
var response = content != null? await client.PostAsync(url, content): await client.GetAsync(url);
game.Send($"{StaticHandleS.HintText} response.StatusCode{response.StatusCode}");
return await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
game.Send($"{StaticHandleS.HintText} 网络发生错误,类型:{ex.GetType().Name},消息:{ex.Message}");
return null;
}
}
public static async Task DoLogin(SingleGame game)
{
var client = new HttpClient();
client.DefaultRequestVersion = HttpVersion.Version20;
client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0");
string? need_web = null;
client.Timeout = TimeSpan.FromSeconds(3);
game.Send($"{StaticHandleS.HintText} 尝试登玩家{game.account.nickName}到{ServicesStaticInfo.ServicesName[game.account.providerId]}的{ServicesStaticInfo.ServerNames[game.account.serverId]}服");
switch (game.account.providerId)
{
case 0:
case 1:
need_web = "https://www.917play.com.tw/ddt_webserver";
break;
default:
game.Send($"{StaticHandleS.HintText} 错误。未适配的运营商:{game.account.serverId}");
break;
}
client.Dispose();
if (need_web == null) return;
//for (int i = 6; i > 0; i--)
//{
// await Task.Delay(1000);
// if (!game.Send($"{StaticHandleS.HintText} 自动登录失败,将在{i}后启用网页登录,关闭窗口取消")) { return; }
//}
game.Send($"{StaticHandleS.UseBrowser} {need_web} {game.account.providerId + 4} {ServicesStaticInfo.ServerIds[game.account.serverId]} {game.account.userName} {game.account.userPWD}");
}
}
}