diff --git a/STranslate/Utils/HttpUtil.cs b/STranslate/Utils/HttpUtil.cs index 183dd0a..9245a8f 100644 --- a/STranslate/Utils/HttpUtil.cs +++ b/STranslate/Utils/HttpUtil.cs @@ -61,11 +61,18 @@ namespace STranslate.Utils { using (var client = new HttpClient()) { - var respContent = await client.GetAsync(urlpath); + try + { + var respContent = await client.GetAsync(urlpath); - string respStr = await respContent.Content.ReadAsStringAsync(); + string respStr = await respContent.Content.ReadAsStringAsync(); - return respStr; + return respStr; + } + catch (Exception ex) + { + throw ex; + } } } } diff --git a/STranslate/Utils/TranslateUtil.cs b/STranslate/Utils/TranslateUtil.cs index e7706db..d707d99 100644 --- a/STranslate/Utils/TranslateUtil.cs +++ b/STranslate/Utils/TranslateUtil.cs @@ -61,16 +61,18 @@ namespace STranslate.Utils /// public static async Task TranslateBaiduAsync(string appID, string secretKey, string text, LanguageEnum target, LanguageEnum source = LanguageEnum.AUTO) { - Random rd = new Random(); - string salt = rd.Next(100000).ToString(); - string sign = EncryptString(appID + text + salt + secretKey); - string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?"; - url += "q=" + HttpUtility.UrlEncode(text); - url += "&from=" + source.ToString().ToLower(); - url += "&to=" + target.ToString().ToLower(); - url += "&appid=" + appID; - url += "&salt=" + salt; - url += "&sign=" + sign; + try + { + Random rd = new Random(); + string salt = rd.Next(100000).ToString(); + string sign = EncryptString(appID + text + salt + secretKey); + string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?"; + url += "q=" + HttpUtility.UrlEncode(text); + url += "&from=" + source.ToString().ToLower(); + url += "&to=" + target.ToString().ToLower(); + url += "&appid=" + appID; + url += "&salt=" + salt; + url += "&sign=" + sign; #if false HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; @@ -84,14 +86,19 @@ namespace STranslate.Utils myStreamReader.Close(); myResponseStream.Close(); #endif - - var retString = await HttpUtil.GetAsync(url); - var resp = JsonConvert.DeserializeObject(retString); - if (resp.From != null) + + var retString = await HttpUtil.GetAsync(url); + var resp = JsonConvert.DeserializeObject(retString); + if (resp.From != null) + { + return resp.TransResult[0]?.Dst; + } + return string.Empty; + } + catch (Exception ex) { - return resp.TransResult[0]?.Dst; + throw ex; } - return string.Empty; } // 计算MD5值 public static string EncryptString(string str) diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index 290b258..0f6ef41 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -19,31 +19,31 @@ namespace STranslate.ViewModel private string Text; public MainVM() { - try - { - //初始化界面参数 - InputCombo = LanguageEnumDict.Keys.ToList(); - InputComboSelected = LanguageEnum.AUTO.GetDescription(); - OutputCombo = LanguageEnumDict.Keys.ToList(); - OutputComboSelected = LanguageEnum.EN.GetDescription(); + //初始化界面参数 + InputCombo = LanguageEnumDict.Keys.ToList(); + InputComboSelected = LanguageEnum.AUTO.GetDescription(); + OutputCombo = LanguageEnumDict.Keys.ToList(); + OutputComboSelected = LanguageEnum.EN.GetDescription(); - config = ConfigUtil.ReadConfig(ConfigPath); + config = ConfigUtil.ReadConfig(ConfigPath); - //手动复制翻译结果 - CopyTranslateResultCmd = new RelayCommand((_) => - { - return string.IsNullOrEmpty(OutputTxt) ? false : true; - }, (_) => - { - System.Diagnostics.Debug.Print("手动复制翻译结果: " + OutputTxt); - Clipboard.SetText(OutputTxt); - }); + //手动复制翻译结果 + CopyTranslateResultCmd = new RelayCommand((_) => + { + return string.IsNullOrEmpty(OutputTxt) ? false : true; + }, (_) => + { + System.Diagnostics.Debug.Print("手动复制翻译结果: " + OutputTxt); + Clipboard.SetText(OutputTxt); + }); - TranslateCmd = new RelayCommand((_) => - { - return string.IsNullOrEmpty(InputTxt) ? false : true; - }, async (_) => + TranslateCmd = new RelayCommand((_) => + { + return string.IsNullOrEmpty(InputTxt) ? false : true; + }, async (_) => + { + try { Text = InputTxt; @@ -63,14 +63,12 @@ namespace STranslate.ViewModel return; } OutputTxt = translateResp; - Clipboard.SetText(OutputTxt); - }); - - } - catch (Exception ex) - { - OutputTxt = ex.Message; - } + } + catch (Exception ex) + { + OutputTxt = ex.Message; + } + }); } public ICommand TranslateCmd { get; private set; }