fix: catch not connect to network exception

dev_without_third_libraries
ZGGSONG 2 years ago
parent d94b20f39b
commit 866d896df8

@ -61,11 +61,18 @@ namespace STranslate.Utils
{ {
using (var client = new HttpClient()) 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;
}
} }
} }
} }

@ -61,16 +61,18 @@ namespace STranslate.Utils
/// <returns></returns> /// <returns></returns>
public static async Task<string> TranslateBaiduAsync(string appID, string secretKey, string text, LanguageEnum target, LanguageEnum source = LanguageEnum.AUTO) public static async Task<string> TranslateBaiduAsync(string appID, string secretKey, string text, LanguageEnum target, LanguageEnum source = LanguageEnum.AUTO)
{ {
Random rd = new Random(); try
string salt = rd.Next(100000).ToString(); {
string sign = EncryptString(appID + text + salt + secretKey); Random rd = new Random();
string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?"; string salt = rd.Next(100000).ToString();
url += "q=" + HttpUtility.UrlEncode(text); string sign = EncryptString(appID + text + salt + secretKey);
url += "&from=" + source.ToString().ToLower(); string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
url += "&to=" + target.ToString().ToLower(); url += "q=" + HttpUtility.UrlEncode(text);
url += "&appid=" + appID; url += "&from=" + source.ToString().ToLower();
url += "&salt=" + salt; url += "&to=" + target.ToString().ToLower();
url += "&sign=" + sign; url += "&appid=" + appID;
url += "&salt=" + salt;
url += "&sign=" + sign;
#if false #if false
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET"; request.Method = "GET";
@ -85,13 +87,18 @@ namespace STranslate.Utils
myResponseStream.Close(); myResponseStream.Close();
#endif #endif
var retString = await HttpUtil.GetAsync(url); var retString = await HttpUtil.GetAsync(url);
var resp = JsonConvert.DeserializeObject<BaiduResp>(retString); var resp = JsonConvert.DeserializeObject<BaiduResp>(retString);
if (resp.From != null) 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值 // 计算MD5值
public static string EncryptString(string str) public static string EncryptString(string str)

@ -19,31 +19,31 @@ namespace STranslate.ViewModel
private string Text; private string Text;
public MainVM() public MainVM()
{ {
try //初始化界面参数
{ InputCombo = LanguageEnumDict.Keys.ToList();
//初始化界面参数 InputComboSelected = LanguageEnum.AUTO.GetDescription();
InputCombo = LanguageEnumDict.Keys.ToList(); OutputCombo = LanguageEnumDict.Keys.ToList();
InputComboSelected = LanguageEnum.AUTO.GetDescription(); OutputComboSelected = LanguageEnum.EN.GetDescription();
OutputCombo = LanguageEnumDict.Keys.ToList();
OutputComboSelected = LanguageEnum.EN.GetDescription();
config = ConfigUtil.ReadConfig(ConfigPath); config = ConfigUtil.ReadConfig(ConfigPath);
//手动复制翻译结果 //手动复制翻译结果
CopyTranslateResultCmd = new RelayCommand((_) => CopyTranslateResultCmd = new RelayCommand((_) =>
{ {
return string.IsNullOrEmpty(OutputTxt) ? false : true; return string.IsNullOrEmpty(OutputTxt) ? false : true;
}, (_) => }, (_) =>
{ {
System.Diagnostics.Debug.Print("手动复制翻译结果: " + OutputTxt); System.Diagnostics.Debug.Print("手动复制翻译结果: " + OutputTxt);
Clipboard.SetText(OutputTxt); Clipboard.SetText(OutputTxt);
}); });
TranslateCmd = new RelayCommand((_) => TranslateCmd = new RelayCommand((_) =>
{ {
return string.IsNullOrEmpty(InputTxt) ? false : true; return string.IsNullOrEmpty(InputTxt) ? false : true;
}, async (_) => }, async (_) =>
{
try
{ {
Text = InputTxt; Text = InputTxt;
@ -63,14 +63,12 @@ namespace STranslate.ViewModel
return; return;
} }
OutputTxt = translateResp; OutputTxt = translateResp;
Clipboard.SetText(OutputTxt); }
}); catch (Exception ex)
{
} OutputTxt = ex.Message;
catch (Exception ex) }
{ });
OutputTxt = ex.Message;
}
} }
public ICommand TranslateCmd { get; private set; } public ICommand TranslateCmd { get; private set; }

Loading…
Cancel
Save