diff --git a/src/MaaWpfGui/Res/Localizations/en-us.xaml b/src/MaaWpfGui/Res/Localizations/en-us.xaml index 7b47963ec4..e06907a5d7 100644 --- a/src/MaaWpfGui/Res/Localizations/en-us.xaml +++ b/src/MaaWpfGui/Res/Localizations/en-us.xaml @@ -676,7 +676,7 @@ The video aspect ratio needs to be 16:9 without interference factors such as bla Get Task Endpoint Report Task Endpoint Connection Test Successful! - Connection Test Failed. + Connection Test Failed, reason: {0}. Connection Test Failed, Endpoint is empty. Connection Test Failed. Endpoint is not an https address. Received remote task {0}, Task Id:{1}. diff --git a/src/MaaWpfGui/Res/Localizations/ja-jp.xaml b/src/MaaWpfGui/Res/Localizations/ja-jp.xaml index b6751ea5de..049b364d45 100644 --- a/src/MaaWpfGui/Res/Localizations/ja-jp.xaml +++ b/src/MaaWpfGui/Res/Localizations/ja-jp.xaml @@ -676,7 +676,7 @@ Bilibili: ログイン インターフェイスに表示されるアカウント タスクエンドポイントの取得 タスクステータスの報告エンドポイント 接続テスト成功! - 接続テスト失敗。 + 接続テスト失敗、理由: {0}。 接続テスト失敗、エンドポイントが空です。 接続テスト失敗。エンドポイントはhttpsアドレスではありません。 リモートタスク{0}を受信、タスクId:{1}。 diff --git a/src/MaaWpfGui/Res/Localizations/ko-kr.xaml b/src/MaaWpfGui/Res/Localizations/ko-kr.xaml index 16658b9f56..6ce1ed281b 100644 --- a/src/MaaWpfGui/Res/Localizations/ko-kr.xaml +++ b/src/MaaWpfGui/Res/Localizations/ko-kr.xaml @@ -676,7 +676,7 @@ Bilibili: 로그인 인터페이스에 표시되는 계정 이름(예: 장산) 작업 엔드포인트 가져오기 작업 상태 보고 엔드포인트 연결 테스트 성공! - 연결 테스트 실패. + 연결 테스트 실패, 이유: {0}. 연결 테스트 실패, 엔드포인트가 비어 있습니다. 연결 테스트 실패. 엔드포인트가 https 주소가 아닙니다. 원격 작업 {0} 받음, 작업 Id:{1}. diff --git a/src/MaaWpfGui/Res/Localizations/zh-cn.xaml b/src/MaaWpfGui/Res/Localizations/zh-cn.xaml index 72233b1dd7..ba4a5f9551 100644 --- a/src/MaaWpfGui/Res/Localizations/zh-cn.xaml +++ b/src/MaaWpfGui/Res/Localizations/zh-cn.xaml @@ -676,7 +676,7 @@ 获取任务端点 汇报任务端点 连接测试成功! - 连接测试失败。 + 连接测试失败,原因: {0}。 连接测试失败,连接端点为空。 连接测试失败。端点不是https地址。 收到远端任务{0},任务Id:{1}。 diff --git a/src/MaaWpfGui/Res/Localizations/zh-tw.xaml b/src/MaaWpfGui/Res/Localizations/zh-tw.xaml index 7d4a9dedad..643d1a714c 100644 --- a/src/MaaWpfGui/Res/Localizations/zh-tw.xaml +++ b/src/MaaWpfGui/Res/Localizations/zh-tw.xaml @@ -676,7 +676,7 @@ 獲取任務端點 匯報任務端點 連接測試成功! - 連接測試失敗。 + 連接測試失敗,原因: {0}。 連接測試失敗,連接端點為空。 連接測試失敗。端點非https地址。 收到遠端任務{0},任務Id:{1}。 diff --git a/src/MaaWpfGui/Services/RemoteControl/RemoteControlService.cs b/src/MaaWpfGui/Services/RemoteControl/RemoteControlService.cs index 35c01cebca..5cf582bca6 100644 --- a/src/MaaWpfGui/Services/RemoteControl/RemoteControlService.cs +++ b/src/MaaWpfGui/Services/RemoteControl/RemoteControlService.cs @@ -3,18 +3,22 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net.Http; using System.Reflection; +using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media.Imaging; using System.Windows.Threading; using MaaWpfGui.Constants; using MaaWpfGui.Helper; +using MaaWpfGui.Services.Web; using MaaWpfGui.States; using MaaWpfGui.ViewModels.UI; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Serilog; +using Windows.Media.Protection.PlayReady; using static System.Windows.Forms.VisualStyles.VisualStyleElement; namespace MaaWpfGui.Services.RemoteControl @@ -70,6 +74,34 @@ namespace MaaWpfGui.Services.RemoteControl #region Private Method Invoker + private static T GetPrivateFieldValue(object instance, string fieldName) + { + if (instance == null) + { + throw new ArgumentNullException(nameof(instance)); + } + + if (string.IsNullOrEmpty(fieldName)) + { + throw new ArgumentNullException(nameof(fieldName)); + } + + Type type = instance.GetType(); + FieldInfo fieldInfo = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); + + if (fieldInfo == null) + { + throw new ArgumentException($"Field '{fieldName}' not found in type '{type.FullName}'."); + } + + if (!typeof(T).IsAssignableFrom(fieldInfo.FieldType)) + { + throw new ArgumentException($"Field '{fieldName}' is not of type {typeof(T)}."); + } + + return (T)fieldInfo.GetValue(instance); + } + private static void InvokeInstanceMethod(object instance, string methodName) { if (instance == null) @@ -500,14 +532,52 @@ namespace MaaWpfGui.Services.RemoteControl var uid = Instances.SettingsViewModel.RemoteControlUserIdentity; var did = Instances.SettingsViewModel.RemoteControlDeviceIdentity; - var response = await Instances.HttpService.PostAsJsonAsync(new Uri(endpoint), new { user = uid, device = did }); - using var toast = new ToastNotification( - LocalizationHelper.GetString( - response != null ? "RemoteControlConnectionTestSuccess" : "RemoteControlConnectionTestFail")); + try + { + var body = System.Text.Json.JsonSerializer.Serialize(new { user = uid, device = did }); + var message = new HttpRequestMessage(HttpMethod.Post, endpoint); + message.Headers.Accept.ParseAdd("application/json"); + message.Content = new StringContent(body, Encoding.UTF8, "application/json"); - toast.Show(); + var client = GetPrivateFieldValue(Instances.HttpService, "_client"); + var response = await client.SendAsync(message); + if (response != null) + { + if (!response.IsSuccessStatusCode) + { + var errorMsg = string.Format(LocalizationHelper.GetString("RemoteControlConnectionTestFail"), response.StatusCode); + using var toastFail = new ToastNotification(errorMsg); + toastFail.Show(); + return; + } + } + else + { + // 一般来说不会走到这里,因为null response一定会报错 + var errorMsg = string.Format(LocalizationHelper.GetString("RemoteControlConnectionTestFail"), "Unknown"); + using var toastFail = new ToastNotification(errorMsg); + toastFail.Show(); + return; + } + + using var toastSuccess = new ToastNotification( + LocalizationHelper.GetString("RemoteControlConnectionTestSuccess")); + toastSuccess.Show(); + } + catch (Exception e) + { + var error = e.Message; + if (e.InnerException != null) + { + error = e.InnerException.Message; + } + var errorMsg = string.Format(LocalizationHelper.GetString("RemoteControlConnectionTestFail"), error); + using var toastErr = new ToastNotification(errorMsg); + + toastErr.Show(); + } } public static void RegenerateDeviceIdentity()